Skip to main content
Became Hot Network Question
added 242 characters in body
Source Link

Update 2: I'm working on macOS with bash 3.2, which is why the snippet above from man bash fails to mention “ except the command following the final && or ||”. 😵‍💫 Thanks again to @Kusalananda for providing the missing details.

Update 2: I'm working on macOS with bash 3.2, which is why the snippet above from man bash fails to mention “ except the command following the final && or ||”. 😵‍💫 Thanks again to @Kusalananda for providing the missing details.

Added an update with a second example
Source Link

Update: the first answer by @Kusalananda is correct for the example above (adding return 0 to the control_operators function prevents the ERR signal). However, here is another example which seems to violate the “part of a && or || list” rule:

#!/usr/bin/env bash
_trap_err() {
    local status=$? sig=$1 line=$2;
    echo "Exit status ${status} on line ${line}: \`${BASH_COMMAND}\`";
}
trap '_trap_err ERR $LINENO' ERR;
true && false;
echo Done;

The output of this script is:

❯ test.sh
Exit status 1 on line 7: `false`
Done

The line true && false should not send an ERR signal because the command false is “part of a && or || list”. So, why did it?


Update: the first answer by @Kusalananda is correct for the example above (adding return 0 to the control_operators function prevents the ERR signal). However, here is another example which seems to violate the “part of a && or || list” rule:

#!/usr/bin/env bash
_trap_err() {
    local status=$? sig=$1 line=$2;
    echo "Exit status ${status} on line ${line}: \`${BASH_COMMAND}\`";
}
trap '_trap_err ERR $LINENO' ERR;
true && false;
echo Done;

The output of this script is:

❯ test.sh
Exit status 1 on line 7: `false`
Done

The line true && false should not send an ERR signal because the command false is “part of a && or || list”. So, why did it?

Update output to show correct line number of the error
Source Link
❯ test.sh
Exit status 1 on line 1314: `[[ 1 -eq 2 ]]`
Done
❯ test.sh
Exit status 1 on line 13: `[[ 1 -eq 2 ]]`
Done
❯ test.sh
Exit status 1 on line 14: `[[ 1 -eq 2 ]]`
Done
Source Link
Loading