bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Incrementing variable=0 with arithmetic expansion causes Return code


From: Ilkka Virta
Subject: Re: Incrementing variable=0 with arithmetic expansion causes Return code = 1
Date: Fri, 28 Aug 2020 18:19:34 +0300

On Fri, Aug 28, 2020 at 4:04 PM Gabriel Winkler <gabriel.winkler@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)).


reply via email to

[Prev in Thread] Current Thread [Next in Thread]