Lets say I have apple.sh

set -e
...some commands #X...
...more commands #Y..

and then banana.sh

set -e
...some commands #Z...

My expectation was that if an error was thrown somewhere in banana.sh, that this would quit banana.sh in an error state, and therefore apple.sh would also see the error and quit. I feel like I've even seen that be the behavior. But now I am seeing that this is not true, and while banana.sh quits as expected when an error occurs, apple.sh barges on from there continuing on with the "some commands #Y" stuff.

How can I make the error in banana bubble up and be caught by apple?

This is on RHEL5

  • 2
    This should have happened just exactly according to your expectation. If ./banada.sh exits with an error than apple.sh exits too. You should debug this by inserting echo $? just after the invocation of ./banada.sh in apple.sh so see if the banada is really failing. – Celada Jun 2 '15 at 16:35

This might not be your case, but did you call banana.sh in a specific context where -e is being ignored?

In the Bash reference manual you can read a detailed explanation about such a context, with the important info as follows:

If a compound command or shell function executes in a context where -e is being ignored, none of the commands executed within the compound command or function body will be affected by the -e setting, even if -e is set and a command returns a failure status. If a compound command or shell function sets -e while executing in a context where -e is ignored, that setting will not have any effect until the compound command or the command containing the function call completes.

This means if you have the following apple.sh:

set -e

func() {
        echo '...some commands #X...'
        false # Suppose ./banana.sh fails here
        echo '...some commands #Y...'

if func; then
        echo 'func() returns success'
        echo 'func() returns failure'

You'll get the following output:

...some commands #X...
...some commands #Y...
func() returns success

Not only bash, but other shells including dash and zsh will give the same result.

To answer your question, there's apparently no way to change the behavior at least on bash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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