bug-bash
[Top][All Lists]
Advanced

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

Re: Question on $@ vs $@$@


From: Greg Wooledge
Subject: Re: Question on $@ vs $@$@
Date: Tue, 13 Aug 2024 22:17:00 -0400

On Wed, Aug 14, 2024 at 02:45:25 +0200, Steffen Nurpmeso wrote:
> I include bug-bash even though i think bash is correct, but there
> lots of people of expertise are listening, so, thus.
> Sorry for cross-posting, nonetheless.
> Given this snippet (twox() without argument it is)
> 
>   one() { echo "$# 1<$1>"; }
>   two() { one "$@"; }
>   twox() { one "$@$@"; }
>   two
>   two x
>   twox
>   twox x

So... what's the question?  You didn't actually ask anything.

As far as I can tell from the bash man page, "$@$@" does not appear to
be well-defined.  From the man page:

       @      Expands to the positional parameters,  starting  from  one.   In
              contexts  where  word  splitting is performed, this expands each
              positional parameter to a separate word; if  not  within  double
              quotes,  these words are subject to word splitting.  In contexts
              where word splitting is not performed, this expands to a  single
              word  with each positional parameter separated by a space.  When
              the expansion occurs within double quotes,  each  parameter  ex‐
              pands  to  a separate word.  That is, "$@" is equivalent to "$1"
              "$2" ...  If the double-quoted expansion occurs within  a  word,
              the  expansion  of the first parameter is joined with the begin‐
              ning part of the original word, and the expansion  of  the  last
              parameter  is  joined  with  the last part of the original word.
              When there are no positional parameters, "$@" and $@  expand  to
              nothing (i.e., they are removed).

We know what "$@" is supposed to do.  And something like "x${@}y" is
well-defined also -- you simply prefix "x" to the first word, and append
"y" to the final word.

But I don't know how "$@$@" is supposed to be interpreted.  I do not see
anything in the official wording that explains how it should work.

Therefore, *my* question is: what are you trying to do?

Given a series of positional parameters, such as

    set -- '' "two words" foobar

what do you expect "$@$@" to expand to?  Bash 5.2 gives me

    hobbit:~$ set -- '' "two words" foobar
    hobbit:~$ printf '<%s> ' "$@$@"; echo
    <> <two words> <foobar> <two words> <foobar> 

which appears to concatenate the last word of the list and the first
word of the list -- a reasonable output, I would say.  Here's a clearer
look:

    hobbit:~$ set -- a '' b
    hobbit:~$ printf '<%s> ' "$@$@"; echo
    <a> <> <ba> <> <b>

I can't complain about this result.  But at the same time, I can't say
"this is the best possible result".  Other interpretations seem equally
valid.  I just wonder what your intent was, in using the "$@$@" expansion
in the first place.



reply via email to

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