I thought setting IFS to $'\n'
would help me in reading an entire file into an array, as in:
IFS=$'\n' read -r -a array < file
However, the above command only reads the first line of the file into the first element of the array, and nothing else.
Even this reads only the first line into the array:
string=$'one\ntwo\nthree'
IFS=$'\n' read -r -a array <<< "$string"
I came across other posts on this site that talk about either using mapfile -t
or a read
loop to read a file into an array.
Now my question is: when do I use IFS=$'\n'
at all?
Your second try almost works, but you have to tell read
that it should not just read until newline (the default behaviour), but for example until the null string:
$ IFS=$'\n' read -a arr -d '' <<< $'a b c\nd e f\ng h i'
$ declare -p arr
declare -a arr='([0]="a b c" [1]="d e f" [2]="g h i")'
But as you pointed out, mapfile
/readarray
is the way to go if you have it (requires Bash 4.0 or newer):
$ mapfile -t arr <<< $'a b c\nd e f\ng h i'
$ declare -p arr
declare -a arr='([0]="a b c" [1]="d e f" [2]="g h i")'
The -t
option removes the newlines from each element.
As for when you'd want to use IFS=$'\n'
:
IFS
without a space to avoid unexpected side effects from word splitting; the proper approach in my opinion, though, is to understand word splitting and make sure to avoid it with proper quoting as desired.IFS=$'\n'
used in tab completion scripts, for example the one for cd
in bash-completion: this script fiddles with paths and replaces colons with newlines, to then split them up using that IFS
.