I need to read the lines of a text file into an array in Bash. The simple approach
arr=( $(cat myfile.txt) )
does not work for me, because the lines may contain whitespace, so an input like this
foo bar
baz
would result in an array
[ "foo", "bar", "baz" ]
instead of the desired
[ "foo bar", "baz" ]
I can get the desired result by using a loop like this:
declare -a arr=()
while read -r line; do
arr+=( "$line" )
done < myfile.txt
but that seems a bit overkill.