Re: "$@" vs. nounset

2009-07-03 Thread Andreas Schwab
Yang Zhang  writes:

> As a result I'm forced to use "${@:-}" or something like that

The correct idiom is ${1+"$@"}, which also works around the old Bourne
shell bug that causes "$@" to expand to a single empty argument when
there are no positional arguments.

Andreas.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."




Re: "$@" vs. nounset

2009-06-30 Thread Chet Ramey
Yang Zhang wrote:
> (IIRC, in bash, variables set to
> empty arrays and unset variables are the same).

In the shell, a variable is not set until it has been assigned a value.
An array variable is not set unless one of its indices has been assigned
a value.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer

Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/




Re: "$@" vs. nounset

2009-06-29 Thread Chet Ramey
Mike Frysinger wrote:
> On Monday 29 June 2009 04:54:52 Yang Zhang wrote:
>> Hi, I like using nounset for stricter scripts, but an annoyance is that
>> anytime I use "$@" and it's empty, I get an error, when (to me,
>> cognitively) it is not "unset" (as in someone *forgot* to set it), it's
>> just an empty, which is a common case (IIRC, in bash, variables set to
>> empty arrays and unset variables are the same).
>>
>> As a result I'm forced to use "${@:-}" or something like that everywhere
>> I use "$@" (which is really everywhere).  Is there any other way around
>> this?  Any way to get a more selective nounset?  Thanks in advance.
> 
> if you search the archives, i think the previous discussion on this topic 
> said 
> bash-4's behavior needed to change here

It's not quite that simple.  And the discussion you reference took place
on the austin-group list, not bug-bash.

First of all, the bash-3.x behavior was inconsistent.  $@ did not exit
the shell, but $...@} did.

When I got bug reports about the behavior, I took a look around.  Posix.2
said that set -u applied only to "variables", which have a specific
definition and do not include $@, $* or any other special parameter.  This
was so far from existing practice, I figured that it was another case of
the set -e issue: Posix.2 wanted to standardize existing practice, but
messed up.  So I changed bash-4.0 to match existing shell practice, and
asked for an interpretation.

The interpretation process went back and forth, and we finally decided
that set -u will apply to references to every variable, parameter, and
special parameter *except* $@ and $*.  That's going to be in the next
revision of the standard.

In the meantime, I have a patch in the pipeline that will implement the
behavior described in the Posix interpretation.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer

Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/




Re: "$@" vs. nounset

2009-06-29 Thread Mike Frysinger
On Monday 29 June 2009 14:38:34 Yang Zhang wrote:
> Mike Frysinger wrote:
> > does something like this at the top of the script work ?
> > [ $# -eq 0 ] && set --
>
> But I also would need that at the top of every function, right?

assuming this snippet actually helps, then probably.  but it might be easier 
than replacing all "$@" occurrences ... i'm merely suggesting workarounds 
until bash gets fixed.
-mike


signature.asc
Description: This is a digitally signed message part.


Re: "$@" vs. nounset

2009-06-29 Thread Yang Zhang

Mike Frysinger wrote:

On Monday 29 June 2009 04:54:52 Yang Zhang wrote:

Hi, I like using nounset for stricter scripts, but an annoyance is that
anytime I use "$@" and it's empty, I get an error, when (to me,
cognitively) it is not "unset" (as in someone *forgot* to set it), it's
just an empty, which is a common case (IIRC, in bash, variables set to
empty arrays and unset variables are the same).

As a result I'm forced to use "${@:-}" or something like that everywhere
I use "$@" (which is really everywhere).  Is there any other way around
this?  Any way to get a more selective nounset?  Thanks in advance.


if you search the archives, i think the previous discussion on this topic said 
bash-4's behavior needed to change here


I'm currently using bash 4.



does something like this at the top of the script work ?
[ $# -eq 0 ] && set --
-mike


But I also would need that at the top of every function, right?
--
Yang Zhang
http://www.mit.edu/~y_z/




Re: "$@" vs. nounset

2009-06-29 Thread Mike Frysinger
On Monday 29 June 2009 04:54:52 Yang Zhang wrote:
> Hi, I like using nounset for stricter scripts, but an annoyance is that
> anytime I use "$@" and it's empty, I get an error, when (to me,
> cognitively) it is not "unset" (as in someone *forgot* to set it), it's
> just an empty, which is a common case (IIRC, in bash, variables set to
> empty arrays and unset variables are the same).
>
> As a result I'm forced to use "${@:-}" or something like that everywhere
> I use "$@" (which is really everywhere).  Is there any other way around
> this?  Any way to get a more selective nounset?  Thanks in advance.

if you search the archives, i think the previous discussion on this topic said 
bash-4's behavior needed to change here

does something like this at the top of the script work ?
[ $# -eq 0 ] && set --
-mike


signature.asc
Description: This is a digitally signed message part.


"$@" vs. nounset

2009-06-29 Thread Yang Zhang
Hi, I like using nounset for stricter scripts, but an annoyance is that 
anytime I use "$@" and it's empty, I get an error, when (to me, 
cognitively) it is not "unset" (as in someone *forgot* to set it), it's 
just an empty, which is a common case (IIRC, in bash, variables set to 
empty arrays and unset variables are the same).


As a result I'm forced to use "${@:-}" or something like that everywhere 
I use "$@" (which is really everywhere).  Is there any other way around 
this?  Any way to get a more selective nounset?  Thanks in advance.

--
Yang Zhang
http://www.mit.edu/~y_z/