I don't understand why "${ARRAY[@]}"
gets expanded to multiple words, when it's quoted ("..."
)?
Take this example:
IFS=":" read -ra ARRAY <<< "foo:bar:baz"
for e in "${ARRAY[@]}"; do echo $e; done
foo
bar
baz
Any other variable that I expand in quotes, say "${VAR}"
, results in a single word:
VAR="foo bar baz"
for a in "${VAR}"; do echo $a; done
foo bar baz
Can anyone explain this to a novice Linux user?
${array[@]}
and${array[*]}
are supposed to act analogously to$@
and$*
. That's a thing called "consistency". – Jan 17 '20 at 17:27"${@:2:4}"
vs."${array[@]:2:4}"
or"${@/pat/repl}"
vs"${array[@]/pat/repl}"
vs. their*
forms. – Jan 17 '20 at 17:37