Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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‐
                            tor.

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

share|improve this question

1 Answer 1

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'
waiting
failure

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'   
waiting

Same results with bash or sh or zsh.

share|improve this answer
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 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

 
discard

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.