bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Word splitting for $@ in variable assignment


From: Nora Platiel
Subject: Re: Word splitting for $@ in variable assignment
Date: Fri, 25 Jun 2021 15:15:25 +0200

On 2021-06-24 21:03, Greg Wooledge wrote:
> "$@" expands like "$1 "$2" ... when used in most contexts.  For example,

Yes, that is clear to me.

> 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.

There are others, like `case "$@" in'.

The doc makes a clear distinction about contexts where word splitting is 
performed and contexts where word splitting is not performed.
In the former case "$@" is multiple words while in the latter case it is a 
single word.

https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html

> 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.

I can see why you think that allowing `@' only in contexts where word splitting 
is performed would have been a better design. And I'm not encouraging the use 
of `@' in contexts where word splitting is not performed, since we have `*' 
which makes more sense there.

But at least it is documented, and that part of the doc is clear to me.
The part of the doc that I'm complaining about is this one:

| A variable may be assigned to by a statement of the form
|     name=[value]
| [...]
| Word splitting is not performed, with the exception of "$@" as
| explained below.

https://www.gnu.org/software/bash/manual/html_node/Shell-Parameters.html

In the context of `name=[value]' word splitting is not performed, period, no 
exceptions.
That's why var="$@" behaves as described in the paragraph about $@ under the 
no-word-splitting case (i.e. "$@" is a single word).

Regards,
NP



reply via email to

[Prev in Thread] Current Thread [Next in Thread]