[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Word splitting for $@ in variable assignment
From: |
Robert Elz |
Subject: |
Re: Word splitting for $@ in variable assignment |
Date: |
Fri, 25 Jun 2021 22:32:56 +0700 |
Date: Thu, 24 Jun 2021 21:03:22 -0400
From: Greg Wooledge <greg@wooledge.org>
Message-ID: <YNUrWhiXULc6zEsb@wooledge.org>
| 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.
It isn't bash, it is Bourne sh (of which bash is an implementation, with
extensions) - there are two distinct contexts where expansions happen - one
where multiple words are expected (pathname expansions etc happen) and the
other where only one word is rational.
The rules are different for the two cases. "$@" is simply "$*" in the
latter case (so you're right, it is kind of dumb to write it, and
simply creates confusion).
I would however never call what "$@" does (in the former case) "word
splitting", that isn't what happens at all, nothing is ever split, there
are simply some number ($#) of input words, which are individually quoted
and inserted in whatever list is being created. This one is really a
very simple thing to understand, it is all of the other $@ and $* expansions
that are the weird(er) ones.
kre
ps: "word" above is in the sh syntax meaning, nothing to do with
natural language words.