为什么不bash的标志-e退出时,子shell不成? [英] Why doesn't bash flag -e exit when a subshell fails?
问题描述
我有点困惑在这里。在这里我的目标是有一个非零退出code中的bash脚本退出时,任何在脚本中的命令失败。使用-e标志,我以为用子shell时,这将是情况下,即使。下面是一个简化的例子:
I'm a bit confused here. My goal here is to have the bash script exit with a non-zero exit code when any of the commands within the script fails. Using the -e flag, I assumed this would be the case, even when using subshells. Below is a simplified example:
#!/bin/bash -e
(false)
echo $?
echo "Line reached!"
下面是输出时跑了:
[$]>Tests/Exec/continuous-integration.sh
1
Line reached!
Bash的版本:3.2.25在CentOS
Bash version: 3.2.25 on CentOS
推荐答案
看来好像这是关系到你的庆典
的版本。在我访问时,bash版本3.1.17和3.2.39出现此行为机,庆典4.1.5没有。
It appears as though this is related to your version of bash
. On machines that I have access to, bash version 3.1.17 and 3.2.39 exhibit this behaviour, bash 4.1.5 does not.
虽然丑了一点,在这两个版本的有效的解决方案可能是这样的:
Although a bit ugly, a solution that works in both versions could be something like this:
#!/bin/bash -e
(false) || exit $?
echo $?
echo "Line reached!"
有其中涉及与设置-e
选项错误bash的源更新日志的一些注意事项。
There are some notes in the bash source changelog which related to bugs with the set -e
option.
这篇关于为什么不bash的标志-e退出时,子shell不成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!