I have a bash script that sets -e so the script will exit on any exit status != 0.
I'm trying to do some basic shell arithmetic assigned to variables and sometimes the expression equals 0 which causes the exit status of the let or expr command to be "1".
Here's an example:
#!/bin/bash -ex echo "Test 1" Z=`expr 1 - 1` || true echo "Z will print" let "A=4 - 4" echo "A WILL NEVER PRINT $A" Y=`expr 1 - 1` echo "Y WILL NEVER PRINT $Y" X=$(expr 2 - 2) echo "X WILL NEVER PRINT $X"
The output is:
$ ./test_error.sh + echo 'Test 1' Test 1 ++ expr 1 - 1 + Z=0 + true + echo 'Z will print' Z will print + let 'A=4 - 4'
My question is what's the idiomatic bash scripting way to allow the script to fail on real exit errors and not on basic arithmetic equaling 0. I could suffix all those expressions with:
A=`expr $C - $D` || true
But that seems hacky.