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

前端 未结 3 1449
无人及你
无人及你 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:58

    To simplify EtanReisner's detailed answer, set -e only exits on an 'uncaught' error. In your case:

    echo "about to fail" && /bin/false && echo "foo"
    

    The failing code, /bin/false, is followed by && which tests its exit code. Since && tests the exit code, the assumption is that the programmer knew what he was doing and anticipated that this command might fail. Ergo, the script does not exit.

    By contrast, consider:

    echo "about to fail" && /bin/false
    

    The program does not test or branch on the exit code of /bin/false. So, when /bin/false fails, set -e will cause the script to exit.

    Alternative that exits when /bin/false fails

    Consider:

    set -e
    echo "about to fail" && /bin/false ; echo "foo"
    

    This version will exit if /bin/false fails. As in the case where && was used, the final statement echo "foo" would therefore only be executed if /bin/false were to succeed.

提交回复
热议问题