The following command prints foo bar, as expected:
sh -c 'printf "$@"' -- "foo bar"
However, when called with foo and bar as separate arguments, it only prints foo:
sh -c 'printf "$@"' -- foo bar
(sh and bash have the same behavior here)
This indicates to me that the expression "$@" is turned into multiple parameters, leading to a call of printf foo bar, instead of printf "foo bar".
- Why is this the case? I thought that quotes denote a single string?
- How can i change the
sh -ccommand to get the desired behaviour (printf "foo bar") when passing multiple arguments?
Because
$@is special.Not in the case of
$@.You can use
"$*"to join arguments with space (first character from IFS).From https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05_02 :