Hacker News new | ask | show | jobs
by HellsMaddy 2175 days ago
A bash pitfall which I have experienced but didn’t see mentioned is the behavior of the `set -e` (errexit) option when using command substitution. If you expect failures within the command substitution to cause the script to exit, you’re gonna be confused.

https://twitter.com/hellsmaddy/status/1273744824835796993?s=...

Tl;dr use `shopt -s inherit_errexit`

1 comments

Thanks. I wasn't aware of that option. I've added the following to my Bash-specific shell scripts:

    # Cause command substitution to inherit the value of the `errexit` option.
    # Introduced in Bash 4.4
    if [ "${BASH_VERSINFO[0]}" -gt 4 ] ||
      { [ "${BASH_VERSINFO[0]}" -eq 4 ] && [ "${BASH_VERSINFO[1]}" -ge 4 ]; }; then
        shopt -s inherit_errexit
    fi