There is a file containing strings. How to read these lines from a file into an array in bash?

  • mapfile (or readarray, which is the same thing). - alexlz

2 answers 2

So much easier:

readarray ARRAY < filename 
  • why minus The answer is absolutely correct (for bash 4). I would give the -t option to remove the line feed. - jfs

read:

 index=0 while read line; do array[$index]="$line" index=$(($index+1)) done < filename 

check:

 for ((a=0; a < ${#array[*]}; a++)) do echo "$a: ${array[$a]}" done