While experimenting with bash arrays, I stumbled upon behaviour I find hard to explain.
> arr=("a" "b")
> bash -c "echo ${arr[*]}"
a b
> bash -c "echo ${arr[@]}"
a
The relevant part of the bash manual states:
${!name[@]}, ${!name[*]} : If name is an array variable, expands to the list of array indices (keys) assigned in name. [...] When ‘@’ is used and the expansion appears within double quotes, each key expands to a separate word.
As I understand it, I would expect the latter example to expand from bash -c "echo ${arr[@]}"
to bash -c "echo \"a\" \"b\""
(or even bash -c "echo a b"
) and output a b
in the subshell.
So, which is the correct behaviour? The observed behaviour? The behaviour I expect? Or something entirely different?