bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

-e does not take effects in subshell


From: PRC
Subject: -e does not take effects in subshell
Date: Tue, 11 Aug 2015 11:42:29 +0000

I have a export function looking like:

mybuild()
{
    (
        set -e
        make
        echo "build okay"
    )
}

I wish to use this function this way:

mybuild && do_other_stuff

But whatever (success or failure) make returns "build okay" is always printed and do_other_stuff always gets executed, which is my expectation.

I have found below descriptions on bash manual.

              -e      Exit  immediately  if  a pipeline (which may consist of a single simple command),  a subshell command enclosed in parentheses, or one of the
                      commands executed as part of a command list enclosed by braces (see SHELL GRAMMAR above) exits with a non-zero status.  The shell  does  not
                      exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test following the if
                      or elif reserved words, part of any command executed in a && or || list except the command following the final && or ||, any  command  in  a
                      pipeline  but  the  last,  or  if  the command's return value is being inverted with !.  A trap on ERR, if set, is executed before the shell
                      exits.  This option applies to the shell environment and each subshell environment separately (see COMMAND EXECUTION ENVIRONMENT above), and
                      may cause subshells to exit before executing all the commands in the subshell.

To my understanding, the purpose of these limitations is to prevent the shell exiting if a command fails in conditional testing context such as if, elif, while, isn't it?
My confusion is why the && operator disables `-e' effects in the subshell. It does not make sense to continue executing remaining commands since I wish the subshell to stop on error.

Or I am wrong and the results match the original designs?

BR

reply via email to

[Prev in Thread] Current Thread [Next in Thread]