[Bash] Read a file one line at a time

Created on November 30, 2026

Use while with read to process the lines of a file. Save this sample as fruits.txt.

apple
orange
banana

The loop prints fruit= before each line.

while IFS= read -r line; do
  printf 'fruit=%s\n' "$line"
done < fruits.txt
fruit=apple
fruit=orange
fruit=banana

IFS= prevents trimming leading and trailing whitespace. read -r keeps backslashes literal. The redirection after done supplies the file as input to the loop.

If you need all the lines in an array, you can use Bash's mapfile.

Reference

Bash Shell command