#!/usr/bin/env bash
test_crash ()
{
a=$((009+1))
}
test_crash
if [[ $? -ne 0 ]] ; then
echo "case 1"
fi
if ! test_crash ; then
echo "case 2"
fi
test_crash || echo "case 3"
{ test_crash ; } || echo "case 4"
exit 0
The code above shows the different results of several methods when a
function crashes due to a syntax error.
pc@DESKTOP-0MVRMOU UCRT64 /d/code$ bash test_bash_broke.sh
test_bash_broke.sh: line 5: 009: value too great for base (error token is
"009")
case 1
test_bash_broke.sh: line 5: 009: value too great for base (error token is
"009")
test_bash_broke.sh: line 5: 009: value too great for base (error token is
"009")
test_bash_broke.sh: line 5: 009: value too great for base (error token is
"009")
pc@DESKTOP-0MVRMOU UCRT64 /d/code$
Why is the exception branch executed only in the case of $?, and skipped in
all other cases?