Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From the bash4 manpage on Debian:</p> <pre><code>((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". </code></pre> <p>and also ...</p> <pre><code>-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. </code></pre> <p>So what is happening is <code>((var++))</code> increments var from 0 to 1 and returns 0, causing the overall expression to return non-zero, which triggers <code>errexit</code>.</p> <p>Now for the difference between the two different bash versions: this change in <code>((</code> behavior seems to have occurred between 4.0 and 4.1. In 4.0 <code>((</code> apparently did not trigger errexit. See this <a href="http://tiswww.case.edu/php/chet/bash/NEWS">NEWS</a> file for the details. You'll have to scroll down to line 135 or so. The Changelog from the source distribution seems to confirm this.</p> <p>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:</p> <ul> <li><code>var="$((var+1))"</code>, the portable POSIX <code>sh</code> method</li> <li><code>((var++)) || true</code>, forcing the statement to always have a zero exit status (bash only)</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload