[Bash] Read file lines into an array

Created on November 30, 2026

Use mapfile to load each line of a file as an array element. Start with fruits.txt.

apple
orange
banana

Use -t to remove the trailing newline from each element.

mapfile -t fruits < fruits.txt
printf '%s\n' "${fruits[@]}"

Output

apple
orange
banana

readarray is another name for mapfile, so readarray -t fruits < fruits.txt works as well.

Bash Shell command