What does set -e mean in a bash script?

后端 未结 8 876
醉酒成梦
醉酒成梦 2020-11-22 15:46

I\'m studying the content of this preinst file that the script executes before that package is unpacked from its Debian archive (.deb) file.

The scr

8条回答
  •  隐瞒了意图╮
    2020-11-22 16:10

    cat a.sh
    #! /bin/bash
    
    #going forward report subshell or command exit value if errors
    #set -e
    (cat b.txt)
    echo "hi"
    
    ./a.sh; echo $?
    cat: b.txt: No such file or directory
    hi
    0
    

    with set -e commented out we see that echo "hi" exit status being reported and hi is printed.

    cat a.sh
    #! /bin/bash
    
    #going forward report subshell or command exit value if errors
    set -e
    (cat b.txt)
    echo "hi"
    
    ./a.sh; echo $?
    cat: b.txt: No such file or directory
    1
    

    Now we see b.txt error being reported instead and no hi printed.

    So default behaviour of shell script is to ignore command errors and continue processing and report exit status of last command. If you want to exit on error and report its status we can use -e option.

提交回复
热议问题