On Fri, Jun 25, 2021 at 01:47:01AM +0200, Nora Platiel wrote: > To me, "$@" expanding to multiple words would mean that: > > $ var="$@" foo > > for $# > 0, behaves the same as: > > $ var="$1" "${@:2}" foo > > which is obviously not the case.
"$@" expands like "$1 "$2" ... when used in most contexts. For example, foo --bar "$@" passes the script's arguments along to foo, exactly as received, with the extra --bar argument in front of them. Likewise, args=("$@") stores the script's arguments as the elements of an indexed array. Each argument is retained as a separate word, becoming one element of the new array. var="$@" is simply a special case. It's an assignment *first*, and it's parsed as such. The assignment-ness of the command overrides everything else. It completely changes how the "$@" expansion occurs. Now, instead of expanding like "$1" "$2" ... it expands like "$1 $2 ...". It's completely unique. "$@" does not act like that in any other context. Bash is ALL about these special cases. If you don't like it, don't write code that uses it. In any sensible programming language, var="$@" would have been an error. In bash, it's not. But that doesn't mean you have to *write* it.