How can I populate a bash array with multi-line command output?
For example given this printf command:
$ printf 'a\nb\n\nc\n\nd\ne\nf\n\n'
a
b
c
d
e
f
I would like to have a bash array populated as if I wrote:
$ arr[0]='a
b'
$ arr[1]='c'
$ arr[2]='d
e
f'
and so could loop through it as:
$ for i in "${arr[@]}"; do printf "<%s>\n" "$i"; done
<a
b>
<c>
<d
e
f>
I have tried various incarnations of using a NUL character to separate my intended array fields instead of a blank line as that seems like my best bet but no luck so far, e.g.:
$ IFS=$'\0' declare -a arr="( $(printf 'a\nb\n\0c\n\0d\ne\nf\n\0') )"
$ for i in "${arr[@]}"; do printf "<%s>\n" "$i"; done
<a>
<b>
<c>
<d>
<e>
<f>
I also tried mapfile -d $'\0' but my mapfile doesn't support -d.
I did find that this works:
$ declare -a arr="( $(printf '"a\nb" "c" "d\ne\nf"') )"
$ for i in "${arr[@]}"; do printf "<%s>\n" "$i"; done
<a
b>
<c>
<d
e
f>
but that seems a little clunky and I'd have to escape "s when all I really want it to tell the shell to use some character other than a blank as the array fields separator.
mapfileinbash4.4 supports-dand was released today.