Sample Header Ad - 728x90

bash: is it possible to make `errexit` work inside conditionals?

1 vote
0 answers
77 views
Consider the following script:
#!/bin/bash

set -o errexit -o xtrace

qux() {
        false
        echo QUX
}

quux() {
        qux ||:
        echo QUUX
}

quux
Running this script produces:
+ quux
+ qux
+ false
+ echo QUX
QUX
+ echo QUUX
QUUX
In other words, not only does quux ignore the return code of qux, it also causes qux discard non-zero error codes within itself, a somewhat counterintuitive behavior. Until now, I have been writing scripts under the assumption that set -o errexit terminates the shell whenever a program or function returns a non-zero code. However, I discovered that errexit doesn't work inside conditionals. Instead, it functions like a global switch that conditionals turn off until they are evaluated. Is it possible to make set -o errexit work inside conditionals? Is there a shell that supports it?
Asked by Azat Khabibulin (133 rep)
May 29, 2024, 04:20 PM
Last activity: May 29, 2024, 08:14 PM