Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash exit status of shorthand increment notation

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?

like image 739
Pankrates Avatar asked Mar 15 '14 19:03

Pankrates


2 Answers

a++ is post-increment: it increments after the statement is evaluated. By contrast, ++a increments before. Thus:

$ a=0 ; ((a++)) ; echo $a $?
1 1
$ a=0 ; ((++a)) ; echo $a $?
1 0

In the first case, ((a++)), the arithmetic expression is evaluated first, while a is still zero, yielding a value of zero (and hence a nonzero return status). Then, afterward, a is incremented.

In second case, ((++a)), a is incremented to 1 and then ((...)) is evaluated. Since a is nonzero when the arithmetic expression is evaluated, the return status is zero.

From man bash:

   id++ id--
          variable post-increment and post-decrement
   ++id --id
          variable pre-increment and pre-decrement
like image 108
John1024 Avatar answered Oct 14 '22 11:10

John1024


The exit status of the (()) notation is zero if the arithmetic expression is nonzero, and vice versa.

A=A+1

You assign 1 to A, so the expression evaluates to 1, exit status zero.

A++

POST-increment operator. The expression evaluates to zero, exit status 1, and then A is incremented.

like image 3
SzG Avatar answered Oct 14 '22 09:10

SzG