I'm struggling with this situation:
$ set -- 1 2 3
$ a="$@"
$ echo "$a"
1 2 3
What I find unexpected is the assignment itself.
man bash
says this about the "$@"
expansion:
When the expansion occurs within double quotes, each parameter expands to a separate word.
So this should be analogous to:
b="1" "2" "3"
bash: 2: command not found
And that's what the "$*"
expansion is for, I gather:
When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.
And so this should be correct:
$ set -- 1 2 3
$ a="$*"
$ echo "$a"
1 2 3
So why does "$@"
yield the same? They're supposed to differ in this point. Is this a Bash problem or my misunderstanding?
Shellcheck detects this as SC2124. I can also provide an example that triggers SC2145.
It's observed on:
GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)
4.9.0-6-amd64 #1 SMP Debian 4.9.82-1+deb9u3 (2018-03-02) x86_64 GNU/Linux