Shell scripting: die on any error

后端 未结 3 1756
天命终不由人
天命终不由人 2020-12-23 20:32

Suppose a shell script (/bin/sh or /bin/bash) contained several commands. How can I cleanly make the script terminate if any of the commands has a failing exit status? Obv

3条回答
  •  天命终不由人
    2020-12-23 20:45

    With standard sh and bash, you can

    set -e
    

    It will

    $ help set
    ...
            -e  Exit immediately if a command exits with a non-zero status.
    

    It also works (from what I could gather) with zsh. It also should work for any Bourne shell descendant.

    With csh/tcsh, you have to launch your script with #!/bin/csh -e

提交回复
热议问题