Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #16827

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

From Ilkka Virta <itvirta@gmail.com>
Newsgroups gnu.bash.bug
Subject Re: Incrementing variable=0 with arithmetic expansion causes Return code = 1
Date 2020-08-28 18:19 +0300
Message-ID <mailman.1610.1598627991.2469.bug-bash@gnu.org> (permalink)
References <GVAP278MB0118201C70D7FAF7B4DF612D98520@GVAP278MB0118.CHEP278.PROD.OUTLOOK.COM> <CAMLQOtT+ANtchsDtSWxhZNUaGcp0rd4R5pqsn5wVNUcOCsDjxw@mail.gmail.com>

Show all headers | View raw


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)).

Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Re: Incrementing variable=0 with arithmetic expansion causes Return code = 1 Ilkka Virta <itvirta@gmail.com> - 2020-08-28 18:19 +0300

csiph-web