I want to create a bash array from a NUL separated input (from stdin).
Here's an example:
## Let define this for clarity
$ hd() { hexdump -v -e '/1 "%02X "'; echo ;}
$ echo -en "A B\0C\nD\0E\0" | hd
41 20 42 00 43 0A 44 00 45 00
So this is my input.
Now, working with NUL works fine if not using the -a
of read
command:
$ while read -r -d '' v; do echo -n "$v" | hd; done < <(echo -en "A B\0C\nD\0E\0")
41 20 42
43 0A 44
45
We get the correct values. But I can't store these values using -a
:
$ read -r -d '' -a arr < <(echo -en "A B\0C\nD\0E\0")
$ declare -p arr
declare -a arr='([0]="A" [1]="B")'
Which is obviously not what I wanted. I would like to have:
$ declare -p arr
declare -a arr='([0]="A B" [1]="C
D" [2]="E")'
Is there a way to go with read -a
, and if it doesn't work, why? Do you know a simple way to do this (avoiding the while
loop) ?