Incrementing a variable triggers EXIT in bash 4, but not in bash 3
From the bash4 manpage on Debian:
((expression)) The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION. If the value of the expresâ sion is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to let "expression".
and also ...
-e Exit immediately if a pipeline (which may consist of a single simple command), a subshell command enclosed in parentheses, or one of the commands executed as part of a command list enclosed by braces (see SHELL GRAMMAR above) exits with a non-zero status.
So what is happening is ((var++))
increments var from 0 to 1 and returns 0, causing the overall expression to return non-zero, which triggers errexit
.
Now for the difference between the two different bash versions: this change in ((
behavior seems to have occurred between 4.0 and 4.1. In 4.0 ((
apparently did not trigger errexit. See this NEWS file for the details.You'll have to scroll down to line 135 or so. The Changelog from the sourcedistribution seems to confirm this.
If you just want a variable incremented without using the exit status, there's multiple ways to do it. Maybe some other people could give advice on which is the best, but some possibilities are:
var="$((var+1))"
, the portable POSIXsh
method((var++)) || true
, forcing the statement to always have a zero exit status (bash only)