Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"$@" and ${1+"$@"}

26 views
Skip to first unread message

James

unread,
Jun 20, 2016, 8:59:26 PM6/20/16
to
What's the difference between "$@" and ${1+"$@"}?

$ test.sh aa "bb cc" "" dd
1: aa bb cc dd
=======================
1: aa
2: bb cc
3:
4: dd
=======================
1: aa
2: bb cc
3:
4: dd

$ cat test.sh
n=0
for f in "$*"; do
n=$((++n))
echo $n: $f
done
echo "======================="
n=0
for f in "$@"; do
n=$((++n))
echo $n: $f
done
echo "======================="
n=0
for f in ${1+"$@"}; do
n=$((++n))
echo $n: $f
done

TIA
James

Icarus Sparry

unread,
Jun 20, 2016, 10:08:05 PM6/20/16
to
On Mon, 20 Jun 2016 17:59:23 -0700, James wrote:

> What's the difference between "$@" and ${1+"$@"}?

Nothing these days. Some older implementations of the shell had a bug in
that "$@" would evaluate to "" if there were no parameters, rather than
to nothing (i.e. the difference between a pointer to a NUL character and
a NULL pointer).

${1+"$@"} is a workaround for this bug. If the first parameter is not set
this evaluates to nothing. If the first parameter is set (even if set to
nothing) then the expression is the same as "$@".

Sven Mascheck

unread,
Jun 21, 2016, 8:06:08 PM6/21/16
to
Icarus Sparry wrote:
> James wrote:
>
>> What's the difference between "$@" and ${1+"$@"}?
>
> Nothing these days. [...]

Yes, it's mainly about the earliest of the traditional Bourne shells.

But there's a twist: In the rare case where you make use
of the flag u ("error upon using empty variables") and "$@"
then even numerous modern shells stumble(d) about this.

Here's more than you ever wanted to know about ${1+"$@"}:
http://www.in-ulm.de/~mascheck/various/bourne_args/
0 new messages