Explanation of 'read' behaviour when extracting multi-line output in to an array in bash
by Shaggy1 from LinuxQuestions.org on (#5BQ92)
Code:$ bash --version
GNU bash, version 4.2.53(2)-release (x86_64-unknown-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.6 (Santiago)(Also tried using bash 4.1.2 on a centos 6.8 machine, and on a centos 7 machine using bash 4.2.46)
--------------------
I was writing a bash script which required parsing some multi-line output from some git commands and while attempting to read lines into an array became a bit confused as to the behaviour of 'read' which is the tool I initially tried to use.
I was wondering if someone might be able to explain the behaviour I am seeing with various commands.
The sample command and output I use below is:
Code:$ ls -1
dir1
dir2
dir3
dir4I initially thought I could set IFS and use the read command:
Code:$ IFS=$'\n' read -a arr <<< `ls -1`but this simply reads all the output in to one element:
Code:$ echo "${arr[0]}"
dir1 dir2 dir3 dir4
$ echo "${arr[1]}"And
Code:$ read -d '\n' -a arr <<< `ls -1`does the same thing
However if I do this:
Code:$ IFS=$'\n' arr=(`ls -1`)Each line is read in to a separate element in the array (which is what I wanted and what I expected from the above commands):
Code:$ echo "${arr[0]}"
dir1
$ echo "${arr[1]}"
dir2
$ echo "${arr[2]}"
dir3
$ echo "${arr[3]}"
dir4
Could anyone explain why read -a does not read each line in to a separate element in the array ?
And why using () does ?
Would I need to set IFS differently for the 'read' command to work ?


GNU bash, version 4.2.53(2)-release (x86_64-unknown-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.6 (Santiago)(Also tried using bash 4.1.2 on a centos 6.8 machine, and on a centos 7 machine using bash 4.2.46)
--------------------
I was writing a bash script which required parsing some multi-line output from some git commands and while attempting to read lines into an array became a bit confused as to the behaviour of 'read' which is the tool I initially tried to use.
I was wondering if someone might be able to explain the behaviour I am seeing with various commands.
The sample command and output I use below is:
Code:$ ls -1
dir1
dir2
dir3
dir4I initially thought I could set IFS and use the read command:
Code:$ IFS=$'\n' read -a arr <<< `ls -1`but this simply reads all the output in to one element:
Code:$ echo "${arr[0]}"
dir1 dir2 dir3 dir4
$ echo "${arr[1]}"And
Code:$ read -d '\n' -a arr <<< `ls -1`does the same thing
However if I do this:
Code:$ IFS=$'\n' arr=(`ls -1`)Each line is read in to a separate element in the array (which is what I wanted and what I expected from the above commands):
Code:$ echo "${arr[0]}"
dir1
$ echo "${arr[1]}"
dir2
$ echo "${arr[2]}"
dir3
$ echo "${arr[3]}"
dir4
Could anyone explain why read -a does not read each line in to a separate element in the array ?
And why using () does ?
Would I need to set IFS differently for the 'read' command to work ?