Creating an array from a text file in Bash

Use the mapfile command:

mapfile -t myArray < file.txt

The error is using for — the idiomatic way to loop over lines of a file is:

while IFS= read -r line; do echo ">>$line<<"; done < file.txt

See BashFAQ/005 for more details.

Leave a Comment