Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

2 views
Skip to first unread message

Gabriel Winkler

unread,
Aug 28, 2020, 9:03:43 AM8/28/20
to bug-...@gnu.org
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc -I/home/abuild/rpmbuild/BUILD/bash-4.4 -L/home/abuild/rpmbuild/BUILD/bash-4.4/../readline-7.0
Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-suse-linux-gnu' -DCONF_VENDOR='suse' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I. -I./include -I./lib -fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -g -D_GNU_SOURCE -DRECYCLES_PIDS -Wall -g -Wuninitialized -Wextra -Wno-switch-enum -Wno-unused-variable -Wno-unused-parameter -Wno-parentheses -ftree-loop-linear -pipe -DBNC382214=0 -DIMPORT_FUNCTIONS_DEF=0 -fprofile-use -fprofile-correction
uname output: Linux sophie 4.12.14-150.32-default #1 SMP Thu Aug 1 08:42:52 UTC 2019 (a2a3983) x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-suse-linux-gnu

Bash Version: 4.4
Patch Level: 23
Release Status: release

Description:
If I have a variable with the value 0 assigned to it incrementing it causes the return code to be 1, even though the variable has been incremented successfully. This only happens if the value of the variable is 0. With positive and negative values this bug is no present.

Repeat-By:
# Works fine
test=1
((test++))
echo $?
0
echo $test
2

# Causes error
test=0
((test++))
echo $?
1
echo $test
1


​Best Regards

Gabriel Winkler

_________________________

bpm consult ag
beeline solutions
Gabriel Winkler
DevOps Engineer
Industriestrasse Ost 10
CH-4614 Hägendorf

Telefon +41 61 417 10 68
Web www.bpm.ch<http://www.bpm.ch/>

k...@plushkava.net

unread,
Aug 28, 2020, 9:09:15 AM8/28/20
to bug-...@gnu.org
This isn't a bug. You should write ((++test)) if you want the
post-incremented value to be evaluated.

--
Kerin Millar

Chris Elvidge

unread,
Aug 28, 2020, 9:42:23 AM8/28/20
to bug-...@gnu.org
> ​Best Regards
>
> Gabriel Winkler
>
> _________________________
>
> bpm consult ag
> beeline solutions
> Gabriel Winkler
> DevOps Engineer
> Industriestrasse Ost 10
> CH-4614 Hägendorf
>
> Telefon +41 61 417 10 68
> Web www.bpm.ch<http://www.bpm.ch/>
>

From man bash:

((expression))
The expression is evaluated according to the rules described
below under ARITHMETIC EVALUATION. If the value of the
expression is non-zero, the return status is 0; otherwise the
return status is 1. This is exactly equivalent to
let "expression".



--
Chris Elvidge
England


Ilkka Virta

unread,
Aug 28, 2020, 11:19:53 AM8/28/20
to Gabriel Winkler, bug-...@gnu.org
On Fri, Aug 28, 2020 at 4:04 PM Gabriel Winkler <gabriel...@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)).

Greg Wooledge

unread,
Aug 28, 2020, 11:23:25 AM8/28/20
to bug-...@gnu.org
On Fri, Aug 28, 2020 at 08:00:30AM +0000, Gabriel Winkler wrote:
> # Causes error
> test=0
> ((test++))
> echo $?
> 1
> echo $test
> 1

https://mywiki.wooledge.org/BashFAQ/105

Exercise 1. And 2. And the whole rest of the page.

0 new messages