read -a
is the wrong tool for the job, as you've noticed; it only supports non-NUL delimiters. The appropriate technique is given in BashFAQ #1:
arr=()
while IFS= read -r -d '' entry; do
arr+=( "$entry" )
done
In terms of why read -d '' -a
is the wrong tool: -d
gives read
an argument to use to determine when to stop reading entirely, rather than when to stop reading a single element.
Consider:
while IFS=$'\t' read -d $'\n' words; do
...
done
...this will read words separated by tab characters, until it reaches a newline. Thus, even with read -a
, using -d ''
will read until it reaches a NUL.
What you want, to read until no more content is available and split by NULs, is not a '-d' of NUL, but no end-of-line character at all (and an empty IFS
). This is not something read
's usage currently makes available.