News for hackers. Stuff that matters.
Re:Filenames and Pathnames in Shell: How to Do It Correctly by floodle on Tuesday June 20, 2023 @07:03AM (#36402288)
This doesn't cause an exit for me on GNU bash (3.2.57)
Re:Filenames and Pathnames in Shell: How to Do It Correctly by js2 on Tuesday June 20, 2023 @11:31AM (#36405471)
I copy/pasted the example from the Google bash scripting guide because I was on mobile earlier, but I just confirmed that it exits immediately after the arithmetic operation with bash 5.1.12.
Re:Filenames and Pathnames in Shell: How to Do It Correctly by js2 on Tuesday June 20, 2023 @02:09PM (#36408099)
The change was made with bash-4.1:

  This is a terse description of the new features added
  to bash-4.1 since the release of bash-4.0. [...]

  j. The [[ and (( commands are now subject to the setting
     of `set -e' and the ERR trap."
https://git.savannah.gnu.org/cgit/bash.git/tree/NEWS#n1078

Test program:

  $ cat foo.sh
  #!/bin/bash
  set -eux
  
  echo "$BASH_VERSION"
  
  i=0
  
  (( i++ ))
  echo "?=$? i=$i"
  
  (( i++ ))
  echo "?=$? i=$i"
Running with 3.2.57:

  $ /bin/bash foo.sh
  + echo '3.2.57(1)-release'
  3.2.57(1)-release
  + i=0
  + ((  i++  ))
  + echo '?=1 i=1'
  ?=1 i=1
  + ((  i++  ))
  + echo '?=0 i=2'
  ?=0 i=2
  
Running with 5.1.16:

  $ /opt/homebrew/bin/bash foo.sh
  + echo '5.1.16(1)-release'
  5.1.16(1)-release
  + i=0
  + ((  i++  ))

So the arithmetic operation returns non-zero in both cases where it evaluates to 0, but only under bash >= 4.1 does it cause bash to terminate.

The moral of the story is to be careful with arithmetic operations that might evaluate to zero, or use this:

(( operation )) || true