Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

12
  • 6
    This has nothing to do with functions, but with subshells (ie with your $(...) command substitutions). set -e / -o errexit is not inherited in subshells. Simpler example: bash -c 'set -e; echo $(false; echo survived)'. If you had used var=$(set -e; func2) in "The Line", the "shouldn't be reached" lines wouldn't have been reached. Commented Sep 14, 2019 at 2:09
  • 1
    Another option is to add shopt -s inherit_errexit at the beginning of your script (also enabled in posix mode). Notice that set -e / errexit is independent of the ERR trap and is not affected by set -E / errtrace Commented Sep 14, 2019 at 2:20
  • @mosvy, Thanks, set -e made my script work as intended. But it also made puzzled me a bit more. Why don't I need to add set -e to the line that performs command substitution in func2? Commented Sep 14, 2019 at 2:21
  • 1
    @mosvy, note that inherit_errexit is on when bash is running as sh. I wouldn't say the idea is right, I'm not sure it could ever be implemented with a clear and consistent API. To me, it's better avoided, reserved for the most basic of scripts (i.e. with the real-life definition of script, when you just put in a file a plain sequence of commands run one after the other). Commented Sep 14, 2019 at 6:12
  • 1
    @mosvy You mentioned in a flagging message that I put this question on hold in order to reduce its visibility. This is not so. I put it on hold because I honestly thought it was a duplicate. I'm reopening it now because I believe that you may have a good answer for this question. Commented Sep 14, 2019 at 23:14