help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] set -u in an arithmetic context


From: Eric Blake
Subject: Re: [Help-bash] set -u in an arithmetic context
Date: Thu, 26 Apr 2012 11:01:15 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120329 Thunderbird/11.0.1

On 04/26/2012 10:14 AM, Bill Gradwohl wrote:
> Finding out that -u doesn't work unless the variable is referenced with $
> or ${} is a disappointment.

Especially since 'set -u' _does_ affect arithmetic expansion (although I
couldn't quickly find such a requirement in POSIX, so we may have a hole
in the standard):

$ bash -c 'unset i; echo $(( i )); set -u; echo $(( i ))'
0
bash: i: unbound variable
$ ksh -c 'unset i; echo $(( i )); set -u; echo $(( i ))'
0
ksh: line 1: i: parameter not set

So the problem here is that (()) is behaving differently than $(())
regarding auto-expansion of unset variables into 0.

> 
> 
>> unset i
>> ((! i)) && echo "yes"
>> yes
>>
>> ((! $i)) && echo "yes"
>> bash: ((: ! : syntax error: operand expected (error token is "! ")
>>
>>
> I would prefer a failure in both cases.

So would I, under consistency rules, and to match the fact that ksh does
likewise.  But it looks like we already have that, in my testing:

$ ksh -c 'unset i; if ((i)); then :; fi'
$ ksh -uc 'unset i; if ((i)); then :; fi'
ksh: line 1: i: parameter not set
$ bash -c 'unset i; if ((i)); then :; fi'
$ bash -uc 'unset i; if ((i)); then :; fi'
bash: i: unbound variable
$ echo $BASH_VERSION
4.2.24(1)-release

Could it be a case of you not having the latest bash, and that this was
fixed in the meantime?

-- 
Eric Blake   address@hidden    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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