I have a shell script incrementing a variable like in the example below with set -e:
$ var=0; echo $?
0
$ ((var++)); echo $?
1
$ ((var++)); echo $?
0
$ ((var++)); echo $?
0
$ echo $var; echo $?
3
0
$ bash --version
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$
The script exits unexpectedly. The thing is that the exact same script doesn't exit the same way when running it locally on a MacBook. The bash shell on the MacBook behaves exactly the same way when running the example above.
Does anyone have any clue what is going on here?
#!/bin/bash; set -e; var=0; echo $?; ((var++)); echo $?; ((var++)); echo $?
. I performed some other tests withoutset -e
and it turns out that when incrementing from 0 to 1 the return code is non-zero; when you decrement from 2 to 1 does return zero; decrementing from 0 to -1 returns non-zero and incrementing from -2 to -1 returns 0. – Lambert Apr 14 '16 at 15:15set -e
, BUT, I was surprised by which step was causing the return code to be different than0
. – Marcel Apr 14 '16 at 15:17bash
. When I run the script usingsh
it does not exit. You can force your script to usesh
by adding the following as first line:#!/bin/sh
– Lambert Apr 14 '16 at 15:24