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   ebl...@redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Reply via email to