Unix Shell - What does ${1+"$@"} mean?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > What does ${1+"$@"} mean?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author What does ${1+"$@"} mean?
lihao0129@gmail.com

2007-12-17, 7:21 pm

Dear shell experts:

In programming PERL edition-3 page 488, I saw this line:

eval `exec PERL -S $0 ${1+"$@"}`

I know ${1+"$@"} works just like $* or the like, but I just dont know
how the 1+"$@" thing works around? Can you shed some light to me on
this problem..

Many thanks,
lihao(XC)
Bill Marcum

2007-12-18, 1:41 am

On 2007-12-18, lihao0129@gmail.com <lihao0129@gmail.com> wrote:
>
>
> Dear shell experts:
>
> In programming PERL edition-3 page 488, I saw this line:
>
> eval `exec PERL -S $0 ${1+"$@"}`
>
> I know ${1+"$@"} works just like $* or the like, but I just dont know
> how the 1+"$@" thing works around? Can you shed some light to me on
> this problem..
>

Some shells have a bug where, if a script is invoked with no
arguments, "$@" expands to a single parameter that is a null string.
${1+"@"} is a workaround for that bug.

Stephane Chazelas

2007-12-18, 7:33 am

On Mon, 17 Dec 2007 20:41:25 -0500, Bill Marcum wrote:
> On 2007-12-18, lihao0129@gmail.com <lihao0129@gmail.com> wrote:
> Some shells have a bug where, if a script is invoked with no
> arguments, "$@" expands to a single parameter that is a null string.
> ${1+"@"} is a workaround for that bug.

[...]

Only the Bourne shell shows that behavior, note.

The code above can be improved a lot:

eval 'exec PERL -- "$0" ${1+"$@"}'
if $running_under_some_shell

Because, with -S, PERL will look up $0 in $PATH. But that
shouldn'd be done unless $0 does not exist as a path. And
looking up $0 in $PATH is only for when the script was invoked
as "sh the-script" which doesn't really make sense for a perl
script.

Also, $0 should be quoted as there's no guarantee that it
doesn't contain any space, tab, newline, *, ?... characters.

And it should be preceded by "--" as there's no guarantee that
$0 doesn't start with "-". There's another case where it won't
work correctly and it's if $0 is "-" in which case, without
"-S", PERL will read its instructions from stding instead of the
script called "-".

So, strictly speaking, for completeness it should rather be
something like:

eval '
[ -n "$ZSH_VERSION" ] && alias -g "\${1+\"\$@\"}=\"\$@\""
if [ -x "$0" ]; then
case $0 in
/*) exec PERL "$0" ${1+"$@"};;
*) exec PERL "./$0" ${1+"$@"};;
esac
else
exec PERL -S -- "$0" ${1+"$@"}
fi'
if $running_under_some_shell;

--
Stephane
AZ Nomad

2007-12-19, 1:39 am

On Mon, 17 Dec 2007 17:10:24 -0800 (PST), lihao0129@gmail.com <lihao0129@gmail.com> wrote:
>Dear shell experts:


>In programming PERL edition-3 page 488, I saw this line:


> eval `exec PERL -S $0 ${1+"$@"}`


>I know ${1+"$@"} works just like $* or the like, but I just dont know
>how the 1+"$@" thing works around? Can you shed some light to me on
>this problem..


sheesh. Which character don't you understand? :-p
Wayne

2007-12-19, 7:32 am

AZ Nomad wrote:
> On Mon, 17 Dec 2007 17:10:24 -0800 (PST), lihao0129@gmail.com <lihao0129@gmail.com> wrote:
>
>
>
>
> sheesh. Which character don't you understand? :-p


${var+value} means to evaluate to var IF var is not set, otherwise
evaluate to "value". So "${1+"$@"| will evaluate to $1 if there is
no parameters, resulting in nothing. If $1 is defined then this
evaluates to "$@" instead. The idea is that a few older shells
are broken and if there are no parameters set, "$@" still becomes
a zero-length parameter rather than nothing. If you use a decent
shell, just use "$@" and don't worry about it. If your script may
be used by others with older shells, it pays to play safe and
use the hack.

-Wayne
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com