Why does set -e; true && false && true not exit?

前端 未结 3 1460
无人及你
无人及你 2020-12-07 01:30

According to this accepted answer using the set -e builtin should suffice for a bash script to exit on the first error. Yet, the following script:



        
3条回答
  •  误落风尘
    2020-12-07 01:32

    Because that answer is not sufficiently specific enough.

    It should say (bolded text is my addition):

    # Any subsequent simple commands which fail will cause the shell script to exit immediately

    Since the man page reads thusly:

    -e      Exit  immediately if a simple command (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 in an if statement, part of a && or ││
            list, or if the command’s return value is being inverted
            via  !.   A  trap on ERR, if set, is executed before the
            shell exits.
    

    And SHELL GRAMMAR expands thusly:

    SHELL GRAMMAR
       Simple Commands
           A simple command is a sequence of optional  variable  assignments  fol-
           lowed  by  blank-separated  words and redirections, and terminated by a
           control operator.  The first word specifies the command to be executed,
           and  is  passed  as  argument  zero.  The remaining words are passed as
           arguments to the invoked command.
    
           The return value of a simple command is its exit status,  or  128+n  if
           the command is terminated by signal n.
    

提交回复
热议问题