brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: set -e is switched off inside an if, so check shell exit codes yourself

Shell
~/tips/tip-shell-set-e-if-caveat

set -e

Explains why set -e does not stop a function that runs as an if condition.

set -e looks like a safety net, but it has holes that surprise people.

set -e
check() { false; echo "still runs"; }
if check; then :; fi

When a function is used as the condition of if, while, && or ||, bash ignores set -e inside it. The code above prints still runs even though false failed. The same applies to commands before && and ||. For steps that must not fail, test them explicitly with cmd || exit 1.

Further reading: Bash manual: The Set Builtin

More Shell tips zmv -n '(*).log' '$1.txt' zoxide query -ls project f=$(mktemp --suffix=.json) shopt globskipdots echo $EPOCHSECONDS All Shell tips (78) All topics

Link