Note that there are some explanatory texts on larger screens.

plurals
  1. POIncrementing a variable triggers EXIT in bash 4, but not in bash 3
    text
    copied!<p>Consider this (exemplary) bash script:</p> <pre><code>#!/bin/bash -e errorExit() { echo "" &gt;&amp;2 echo "ERROR (${var_scriptfilename}):" &gt;&amp;2 echo "An unhandled error occurred." &gt;&amp;2 intentionalExit 1 } intentionalExit () { trap - EXIT # Unregister the EXIT trap exit $1 } trap errorExit EXIT # Trap script errors var_scriptfilename="$(basename "$0")" # ==== START OF TEST ==== var_counter=0 ((var_counter++)) echo "var_counter is $var_counter" &gt;&amp;2 # ===== END OF TEST ===== intentionalExit 0 </code></pre> <p>If I run it in Cygwin's bash it produces the intended output:</p> <pre><code>var_counter is 1 </code></pre> <p>However, if I run it on my Debian Squeeze box, which is its intended destination, I end up in the EXIT trap:</p> <pre><code>ERROR (test.increment.sh): An unhandled error occurred. </code></pre> <p>...Why is that?</p> <p>If I remove the -e option it works as expected on both systems, but I want to keep -e in use, obviously.</p> <p>The slightly more cumbersome "universal" variant, <code>var_counter=$(($var_counter+1))</code>, works with -e being set on both shells, but I would prefer to use the first notation (or something similar-looking) since it clearly sticks out as an increment operation when reading the code.</p> <p><code>bash --version</code> on the Cygwin bash says:</p> <pre><code>GNU bash, version 3.2.51(24)-release (i686-pc-cygwin) Copyright (C) 2007 Free Software Foundation, Inc. </code></pre> <p>On Debian, it is:</p> <pre><code>GNU bash, Version 4.1.5(1)-release (x86_64-pc-linux-gnu) Copyright (C) 2009 Free Software Foundation, Inc. </code></pre> <p>I am intrigued as to why this is that way. Does anybody know the cause of this behavior?</p> <p>Also, does anybody know of a similar-looking way to increment a variable in bash that I could use?</p>
 

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