[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Incrementing variable=0 with arithmetic expansion causes Return code
Re: Incrementing variable=0 with arithmetic expansion causes Return code = 1
Fri, 28 Aug 2020 18:19:34 +0300
On Fri, Aug 28, 2020 at 4:04 PM Gabriel Winkler <email@example.com>
> # Causes error
> echo $?
It's not an error, just a falsy exit code. An error would probably give a
But to elaborate on the earlier answers, the value of the post-increment
var++ is the _old_ value of var, even though var itself is incremented as a
Use the pre-increment ++var to get the incremented value as the value of
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;