arraysbashifs

When do I set IFS to a newline in Bash?


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?


Solution

  • 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':