Bash“本地”和“ $()”破坏“ set -e”行为

时间:2018-06-29 09:22:18

标签: bash shell

我想了解原因:

#!/bin/bash -e

main() {
    local a=$(command_that_does_not_exist)

    echo "This should not be printed $a"
}

main

输出:

/tmp/poc.sh: ligne 4: command_that_does_not_exist : commande introuvable
This should not be printed

我希望脚本在第4行停止并仅输出:

/tmp/poc.sh: ligne 4: command_that_does_not_exist : commande introuvable

此外,以下脚本可以正常工作:

#!/bin/bash -e

main() {
    local a
    a=$(command_that_does_not_exist)

    echo "This should not be printed $a"
}

main

0 个答案:

没有答案