66 links
  • Ban's links
  • Home
  • Login
  • RSS Feed
  • ATOM Feed
  • Tag cloud
  • Picture wall
  • Daily
Links per page: 20 50 100
  • `set -e` is not that useful

    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.

    January 5, 2023 at 16:07:42 GMT+1 * - permalink -
    QRCode
    - https://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin
    bash shell programming
Links per page: 20 50 100
Shaarli - The personal, minimalist, super fast, database-free, bookmarking service by the Shaarli community - Help/documentation