From: Greg Wooledge <greg@wooledge.org>
To: dash@vger.kernel.org, busybox@busybox.net, bug-bash@gnu.org,
Steffen Nurpmeso <steffen@sdaoden.eu>
Subject: Re: Question on $@ vs $@$@
Date: Tue, 13 Aug 2024 22:17:00 -0400 [thread overview]
Message-ID: <ZrwTnBvk8_2mdQUS@wooledge.org> (raw)
In-Reply-To: <20240814004525.A7mNIdWf@steffen%sdaoden.eu>
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.
next prev parent reply other threads:[~2024-08-14 2:23 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-14 0:45 Question on $@ vs $@$@ Steffen Nurpmeso
2024-08-14 2:17 ` Greg Wooledge [this message]
2024-08-14 9:04 ` Marc Chantreux
2024-08-14 13:23 ` Greg Wooledge
2024-08-14 14:51 ` Robert Elz
2024-08-15 7:14 ` Marc Chantreux
2024-08-14 14:20 ` Robert Elz
2024-08-14 14:58 ` Oğuz
2024-08-14 15:28 ` Greg Wooledge
2024-08-14 15:57 ` Chet Ramey
2024-08-14 20:05 ` Steffen Nurpmeso
2024-08-15 18:48 ` Steffen Nurpmeso
2024-08-15 21:33 ` Steffen Nurpmeso
[not found] ` <CAALKErGQcz=LS=xC544fXf9OywVmU32s1R-wSKzVTiavQTHZ6Q@mail.gmail.com>
2024-08-27 0:28 ` Steffen Nurpmeso
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZrwTnBvk8_2mdQUS@wooledge.org \
--to=greg@wooledge.org \
--cc=bug-bash@gnu.org \
--cc=busybox@busybox.net \
--cc=dash@vger.kernel.org \
--cc=steffen@sdaoden.eu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.