[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: arithmetic problem, incorrect return code with ((var++)) when var is
From: |
Eduardo Bustamante |
Subject: |
Re: arithmetic problem, incorrect return code with ((var++)) when var is 0 before operation |
Date: |
Sat, 22 Apr 2017 14:02:51 -0500 |
On Sat, Apr 22, 2017 at 12:49 PM, Andrew McGlashan
<andrew.mcglashan@affinityvision.com.au> wrote:
[...]
>
> The return code from ((i++)) operation is different when i has an
> initial value of 0.
This is not a bug.
Please read the Bash reference manual section on conditional
constructs:
https://www.gnu.org/software/bash/manual/bash.html#index-commands_002c-conditional
| The arithmetic expression is evaluated according to the rules
described below (see Shell Arithmetic). 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"
This is also described in other sources:
1. http://wiki.bash-hackers.org/commands/builtin/let#description
2. http://mywiki.wooledge.org/ArithmeticExpression#Arithmetic_Commands
Also see the 1st example in http://mywiki.wooledge.org/BashFAQ/105
("Why doesn't set -e (or set -o errexit, or trap ERR) do what I
expected?"), which mentions this pitfall.
You can also find extensive discussions in the bug-bash archives on
why you shouldn't be using set -e, regardless of how many "sources" on
the web claim that it helps write "robust" or "correct" shell scripts.