All links of one day
in a single page.
<Previous day - Next day>

rss_feedDaily RSS Feed
floral_left The Daily Shaarli floral_right
——————————— January 5, 2023 - Thursday 05, January 2023 ———————————
bash - shell - programming -

Quoting the Bash manual page:

If a compound command or shell function executes in a context where -e is being ignored, none of the commands executed within the compound command or function body will be affected by the -e setting, even if -e is set and a command returns a failure status. If a compound command or shell function sets -e while executing in a context where -e is ignored, that setting will not have any effect until the compound command or the command containing the function call completes.

All this isn't so clear, but it actually means that if a simple command (or a pipeline, etc.) exits with an error code inside a function which the caller checks, that command will not be subject to set -e.

In practice, it pretty much means that using functions renders set -e moot:

set -e

foo() {
  false  # this command returns 1
  true
}

foo  # that one would fail
foo && echo "No errors!"  # would print "No errors!", and pass fine

All this basically means set -e is untrustworthy when using shell functions to factorize the code. Worse, it gives an impression of safety, where there isn't any.

See also SO answer 37191242 and the bash-bug thread linked there for a "rationale" (can't say I understand why they don't introduce a custom shell option for this, though).

[edit] I was confused about set -E, so scrapped it. But it's basically unrelated, only affects ERR traps.

-