当我运行此命令时
set -e; echo $(echo "$-");
我得到himBH
作为输出。我期待字母e
包含在输出中。怎么回事?
我正在使用Ubuntu 16.04.1 LTS GNU bash,版本4.3.46(1)-release(x86_64-pc-linux-gnu)
答案 0 :(得分:1)
除非您处于POSIX模式或使用errexit
shell选项(添加到inherit_errexit
4.4),否则命令替换不会继承bash
选项。
192% bash -ec 'echo "$(echo "$-")"'
hBc
192% bash --posix -ec 'echo "$(echo "$-")"'
ehBc
192% bash -O inherit_errexit -ec 'echo "$(echo "$-")"' # 4.4+
ehBc
答案 1 :(得分:0)
这个问题! 研究了几个小时,直到我找到了 htis。
我无法将 set -e
继承到子 shell。
这是我的概念证明:
#!/usr/bin/env bash
set -euo pipefail
# uncomment to handle failures properly
# shopt -s inherit_errexit
function callfail() {
echo "SHELLOPTS - callfail - $SHELLOPTS" >&2
local value
value=$(fail)
echo "echo will reset the result to 0"
}
function fail() {
echo "SHELLOPTS - fail - $SHELLOPTS" >&2
echo "failing" >&2
return 1
}
function root() {
local hello
hello=$(callfail)
echo "nothing went bad in callfail"
callfail
echo "nothing went bad in callfail"
}
root
没有 shopt -s inherit_errexit
的执行:
$ ./test.sh
SHELLOPTS - callfail - braceexpand:hashall:interactive-comments:nounset:pipefail
SHELLOPTS - fail - braceexpand:hashall:interactive-comments:nounset:pipefail
failing
nothing went bad in callfail
SHELLOPTS - callfail - braceexpand:errexit:hashall:interactive-comments:nounset:pipefail
SHELLOPTS - fail - braceexpand:hashall:interactive-comments:nounset:pipefail
failing
使用 shopt -s inherit_errexit
执行:
$ ./test.sh
SHELLOPTS - callfail - braceexpand:errexit:hashall:interactive-comments:nounset:pipefail
SHELLOPTS - fail - braceexpand:errexit:hashall:interactive-comments:nounset:pipefail
failing