bash 如果任何命令失败,如何撤消“set -e”使bash立即退出的效果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3517162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to undo the effect of "set -e" which makes bash exit immediately if any command fails?
提问by Tianyi Cui
After entering set -e
in an interactive bash shell, bash will exit immediately if any command exits with non-zero. How can I undo this effect?
进入set -e
交互式 bash shell 后,如果任何命令以非零值退出,bash 将立即退出。我怎样才能撤销这个效果?
回答by zwol
With set +e
. Yeah, it's backward that you enableshell options with set -
and disablethem with set +
. Historical raisins, donchanow.
与set +e
. 是啊,这是落后的,你能够用shell选项set -
,并禁止与他们set +
。历史葡萄干,donchanow。
回答by szeryf
It might be unhandy to use set +e
/set -e
each time you want to override it. I found a simpler solution.
每次要覆盖它时都使用set +e
/可能不方便set -e
。我找到了一个更简单的解决方案。
Instead of doing it like this:
而不是这样做:
set +e
command_that_might_fail_but_we_want_to_ignore_it
set -e
you can do it like this:
你可以这样做:
command_that_might_fail_but_we_want_to_ignore_it || true
or, if you want to save keystrokes and don't mind being a little cryptic:
或者,如果你想保存击键并且不介意有点神秘:
command_that_might_fail_but_we_want_to_ignore_it || :
Hope this helps!
希望这可以帮助!