On Fri, Aug 28, 2020 at 4:04 PM Gabriel Winkler <gabriel.wink...@bpm.ch> wrote:
> # Causes error > test=0 > ((test++)) > echo $? > 1 > It's not an error, just a falsy exit code. An error would probably give a message. But to elaborate on the earlier answers, the value of the post-increment expression var++ is the _old_ value of var, even though var itself is incremented as a side effect. Use the pre-increment ++var to get the incremented value as the value of the expression. The exit status of (( )) is one if the arithmetic expression evaluates to zero, which is exactly what happens here. Similarly, a=0; b=$((a++)) results in a=1, b=0. On the other hand, a=0; b=$((++a)) results in a=1, b=1, and so does a=0; b=$((a+=1)).