I'm using sh (dash) on Ubuntu (lucid) and the manpage has this to say about -e:

           -e errexit       If not interactive, exit immediately if any untested command fails.
                            The exit status of a command is considered to be explicitly tested
                            if the command is used to control an if, elif, while, or until; or
                            if the command is the left hand operand of an “&&” or “||” opera‐

What happens when a background command fails? And does bash behave differently in this respect?

1 Answer 1


It's quite simple to test this. With -e:

% bash -e -c 'false & echo waiting; if wait $!; then echo success; else echo failure; fi'

So if a backgrounded command fails, the shell will not exit automatically (-e is not enough).

If you wait outside an explicit test, wait will return the return code of the failed backgrounded process. In this case, if -e is specified, the shell will exit:

% bash -e -c 'false & echo waiting; wait $!; echo returned'   

Same results with bash or sh or zsh.

  • 1
    wait returns the status of the process it waited for. If the subshell fails, the wait fails, too, and makes the whole shell fail.
    – choroba
    Commented Oct 4, 2012 at 11:27
  • @choroba: wow, indeed :-) Commented Oct 4, 2012 at 11:31
  • Now, I hope the answer is correct. Thanks choroba. Commented Oct 4, 2012 at 11:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .