According to my bash man page: If the word is double-quoted ${name[@]} expands each element of name to a separate word.
This usually leads to exactly the expected behavior:
$ a=("foo 1" "bar 2")
$ for i in "${a[@]}"; do echo $i; done
foo 1
bar 2
$ size(){ echo $#;};size "${a[@]}"
2
$ [ "${a[@]}" = "foo 1 bar 2" ]&&echo ok
bash: [: too many arguments
But sometimes it does not:
$ [[ "${a[@]}" == "foo 1 bar 2" ]] && echo ok
ok
$ case "${a[@]}" in "foo 1 bar 2") echo ok; esac
ok
In those cases it seems to get evaluated into a single string. - This makes sense, but is a bit surprising. - I expected it to be equal to "foo 1" "bar 2" and therefore cause a syntax error.
Is there a rule in which context it is evaluated which way? (I couldn't find the right section in the bash man page.)
Additional question: Is there a case in which "${a[@]}" is handled differently from ${a[@]}?
"and use${a[@]}instead of"${a[@]}".case ${a[@]} in "foo 1 bar 2") echo ok; esac-> okset -xvto view how bash processes the commands.