bug-bash
[Top][All Lists]
Advanced

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

Re: set -e yet again (Re: saving bash....)


From: Eric Blake
Subject: Re: set -e yet again (Re: saving bash....)
Date: Fri, 12 Aug 2011 07:01:23 -0600
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110621 Fedora/3.1.11-1.fc14 Lightning/1.0b3pre Mnenhy/0.8.3 Thunderbird/3.1.11

On 08/12/2011 06:51 AM, Greg Wooledge wrote:
On Thu, Aug 11, 2011 at 11:56:10PM -0700, Linda Walsh wrote:
**Exception**
declare -i a
a=0
--
        As a is declared to be an integer, it has the results evaluated
at assignment time.   a=0 is an integer expression that doesn't set
$?=1
Neither should:
((a=0))

a=0 is an assignment.  Assignments always return 0.

No they don't.

readonly a
a=0

sets $? to non-zero.


imadev:~$ ((a=0)); echo $?
1

And here, the same thing, but we return false, because the value was 0.
This is the thing about which you are complaining.  This is also one
of the things I mention on http://mywiki.wooledge.org/BashFAQ/105 in
which I describe how horrible and useless set -e is.

And that point has been made several times in this thread. 'set -e' is a historical wart - bash has it because POSIX requires it. If you want to use bash extensions, then _don't_ use 'set -e', and you don't have to worry about how the unintuitive behavior interacts with extensions.

Greg, you missed one other useful form:

a=$((0))

This is an assignment (sets $? to 0 for all but errors like assigning to a readonly variable) of arithmetic expansion. Also POSIX, and slightly shorter than

: $((a=0))

--
Eric Blake   address@hidden    +1-801-349-2682
Libvirt virtualization library http://libvirt.org



reply via email to

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