This page collects a set of bash scripting idioms and tools for easy reference.
With -e
(errexit
), bash exits immediately if an unchecked command exits with a nonzero value.
A checked command is part of a while
, until
, if
, elif
, or an expression with &&
or ||
.
Everything else is unchecked.
For a pipeline, only the last command is considered. Adding "or no-op", || :
,
ensures a command is safe, e.g., do-something | grep 'may_not_match' || :
.
NB. Calling a bash function in a checked context effectively disables -e
for all commands in the function.
Use the optional shellcheck, check-set-e-suppressed, to alert on this.