bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Execution of subsequent commands after a command crashes.


From: 王伟
Subject: Execution of subsequent commands after a command crashes.
Date: Mon, 2 Feb 2026 21:22:04 +0800

#!/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?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]