To give line of text its own array element, you can use the new (bash v4+)
mapfile command. a.k.a.
readarray.
Code:
mapfile -t LINE < filename #use -t to remove the trailing newlines.
mapfile also has a convenient
-n option, allowing you to explictly set the index number to start on. Using
-n 1 means that
line one = index one, etc.
In older systems you can do something like this:
Code:
while IFS= read -r line ; do
LINE+=( "$line" )
done < filename
I cleared the
IFS setting for read, because otherwise it removes all leading and trailing whitespace from each line. You can of course remove it if this behavior is desirable.
There's also an issue with loops using
read when a file doesn't contain a final newline. The contents get stored in the variable, but the subsequent commands aren't executed. You may need to explicitly save it after the loop with an additional command:
Code:
[[ -n $line ]] && LINE+=( "$line" )
http://mywiki.wooledge.org/BashFAQ/001
And finally a caution: colucix's second example likely does not do what you want. It breaks each line up into individual words and saves them to the array, but then overwrites them again when the next line executes. So only the final line remains contained in the array at the end of the loop.