I noticed an apparent inconsistency in the return status of bash's (( ))
notation.
Consider the following
$> A=0
$> ((A=A+1))
$> echo $? $A
0 1
However using the other well known shorthand increment notation yields:
$> A=0
$> ((A++))
$> echo $? $A
1 1
If one has the builtin set -e
in the script the second notation will cause the script to exit, since the exit status of the ((A++))
returned non-zero. This question was more or less addressed in this related question. But it does not seem to explain the difference in exit status for the two notations ((A=A+1))
and ((A++))
((A++))
seems to return 1
if and only if A
equals 0
. (Disclaimer: I have not done exhaustive tests. Tested in bash 4.1.2 and 4.2.25). So the final question boils down to:
Why does A=0; ((A++))
return 1
?
set -e
, it is impossible to use either shorthand notation safely for a counter, if the counter is expected to potentially take the value of-1
or0
. Is there a way around this? Or should one simply abandon the shorthand notation – Housemother