I'm using the special-case variable $@ to pass arguments from the user into a defined function in a shell script. The Bash manual states that for $@:
If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word.
This leads me to believe I can simply enclose the variable in double quotes with other words. For example: "You said: $@"
. However, this doesn't appear to work as I expected.
I've put together a small shell script to help debug an issue I was having with passing arguments using $@.
#!/usr/bin/env bash
showArgs() {
while [ "$1" != "" ]; do
echo "Arg: $1"
shift
done
}
VAR1="Quoted variable"
VAR2="Unquoted variable"
echo 1
showArgs "Quoted hardcoded"
echo 2
showArgs Unquoted hardcoded
echo 3
showArgs "$VAR1"
echo 4
showArgs $VAR2
echo 5
showArgs $@
echo 6
showArgs "$@"
Invoking it with ./showArgs Foo bar "bax qux"
, each case shows up as I'd expect. However, I'd expect case #6 to be "Foo bar bax qux" as a single argument since it's being passed to the function in double-quotes.
I don't fully understand why and haven't been able to find the answer. Insight would be appreciated!