bug-bash
[Top][All Lists]
Advanced

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

Re: "${x-"$@"}" expansion


From: Greg Wooledge
Subject: Re: "${x-"$@"}" expansion
Date: Wed, 27 Apr 2016 08:37:55 -0400
User-agent: Mutt/1.4.2.3i

On Wed, Apr 27, 2016 at 07:33:25AM -0400, Grisha Levit wrote:
> Sorry that wasn???t very clear. I only included that case to demonstrate that
> seemingly contradictory things are happening:
> 
>    - "address@hidden" expands each positional parameter to a separate word,
>    following the usual "$@" behavior

To be honest, this looks like a bug in the script to me.  The script
author intended ${_+"$@"} surely.  Or more likely ${1+"$@"} which is
an actual feature used in legacy shell scripts.

The fact that "address@hidden" and ${_+"$@"} both give the same thing when $@
is not empty seems like some sort of coincidence.  Or, yet another trap
for the unwary.  "Hey, I tested it and it worked!" ... 2 months later ...
"Shit, it didn't work in all cases."

P.S. dash and ksh93 also give this result.

>    - The usual "$@" behavior is to expand to 0 words if there are no
>    positional params but in this case "address@hidden" expands to an empty 
> string
>    instead

Again, this looks like a bug in the script.  I don't know what it's
supposed to do, but I know it should not have been written this way.

Also again, ${_+"$@"} gets it right.

Also also again, dash and ksh93 give the same result.

I'll leave the "what should the shell do when the script author makes
this particular bug" discussion to the rest of you.  I just want to
advise people to stop writing bugs.  Really, your life gets simpler.

(The fact that bash, dash and ksh93 all agree means it's probably the
prescribed POSIX behavior, but I can't be bothered to research that.
Hell, maybe all 3 shells just happen to have the same bug.)



reply via email to

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