How to convert command output to an array line by line in bash?
You need to change your Internal Field Separator variable (IFS
) to a newline first.
$ IFS=$'\n'; arr=( $(echo -e "a b\nc\nd e") ); for i in ${arr[@]} ; do echo $i ; done
a b
c
d e
readarray -t ARRAY < <(COMMAND)