Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 5 down vote accepted

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.

share|improve this answer
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 Oct 4 '12 at 11:27
@choroba: wow, indeed :-) – Stéphane Gimenez Oct 4 '12 at 11:31
Now, I hope the answer is correct. Thanks choroba. – Stéphane Gimenez Oct 4 '12 at 11:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.