bug-bash
[Top][All Lists]
Advanced

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

arithmetic problem, incorrect return code with ((var++)) when var is 0 b


From: Andrew McGlashan
Subject: arithmetic problem, incorrect return code with ((var++)) when var is 0 before operation
Date: Sun, 23 Apr 2017 03:49:03 +1000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0

Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64'
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu'
-DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash'
-DSHELL -DHAVE_CONFIG_H   -I.  -I../. -I.././include -I.././lib
-Wdate-time -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat
-Werror=format-security -Wall
uname output: Linux andrewm-Satellite-P50-A 4.4.0-72-generic #93-Ubuntu
SMP Fri Mar 31 14:07:41 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 4.3
Patch Level: 42
Release Status: release

Description:

The return code from ((i++)) operation is different when i has an
initial value of 0.

This problem was discovered when running run a bash script with "set
-ue" and having "((i++))" as the last command in a loop.


Repeat-By:

The following script will quit with an error after the first iteration
due to the return code errantly being 1.

#!/bin/bash
set -eu
i=0
for a in a b c
do
   echo "${a}"
   ((i++))
done


NB: If i starts as 1, the script works as expected.

Also, using ((i+=1)) works fine, but I prefer the ((i++)) format.


Proof outside a loop:

$ c=0;((c++));echo $?
1


$ c=1;((c++));echo $?
0





reply via email to

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