[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Strange behaviour of trap ERR. (( x = 0 )) is an error?
From: |
Eduardo Bustamante |
Subject: |
Re: [Help-bash] Strange behaviour of trap ERR. (( x = 0 )) is an error? |
Date: |
Sat, 2 Sep 2017 09:42:34 -0700 |
On Sat, Sep 2, 2017 at 8:54 AM, Andrew S <address@hidden> wrote:
[...]
> BASH_VERSION=[3.00.15(1)-release]
[...]
> BASH_VERSION=[4.1.2(1)-release]
> Questions:
> Why does (( x = 0 )) fire the ERR trap on the new server?
> What can I do to stop (( x = 0 )) from being an error?
> Is there a shell option I have set funny?
FWIW, 4.1.2 is not new. It's from 2009. And 3.0.16 (which means that
the one you were using is even older) is from 2005.
(( x = 0 )) fires the ERR trap, because it's return code is greater
than 0. Which you can see here:
$ (( x = 0 )); echo $?
1
The reason is simple. Both `let' and `((' commands return 0 if the
arithmetical expression evaluates to `expr != 0', otherwise, they
return 1. This is used in constructs like:
if (( expr )); then ...; fi
while (( expr )); then ...; fi
It seems bash 3.0.16 was buggy, because does not exit after the `(('
command returns an error code.
$ ./bash -ec 'echo $BASH_VERSION; (( 0 )); echo $?'
3.00.16(2)-release
1
$ bash -ec 'echo $BASH_VERSION; (( 0 )); echo $?'
4.4.12(1)-release
There's not much you can do here, other than to turn off the ERR trap,
or to disable the errexit/ERR behavior for that particular command
with something like this:
$ bash -ec 'echo $BASH_VERSION; (( 0 )) && :; echo $?'
4.4.12(1)-release
1