You could use split+glob (what happens when you leave an expansion unquoted in list contexts). It gets in our way most of the time, it would be a shame not to use it when we actually need it:
IFS=,
set -o noglob
ARR=($VAR) # split+glob with glob disabled, and split using , as delimiter
That's a bit less convoluted than writing a temp file and then call readarray
on it like in the readarray <<< "$string"
approach (also note that readarray -d
needs a very recent version of bash
).
Note that despite the S
in IFS
(which stands for separator), that works the same way as readarray
in that a,,b,
is split into "a"
, ""
and "b"
only.
For a real splitting operator, you could use zsh
instead:
ARR=("${(@s:,:)VAR}")
(the @
and double quotes to preserve the empty elements).
"What is the meaning of the latest $ symbol?"