How to undo the effect of “set -e” which makes bash exit immediately if any command fails?

余生长醉 提交于 2020-01-22 04:22:48

问题


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?


回答1:


With set +e. Yeah, it's backward that you enable shell options with set - and disable them with set +. Historical raisins, donchanow.




回答2:


It might be unhandy to use set +e/set -e each time you want to override it. I found a simpler solution.

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!




回答3:


  • Using + rather than - causes these flags to be turned off.

Source



来源:https://stackoverflow.com/questions/3517162/how-to-undo-the-effect-of-set-e-which-makes-bash-exit-immediately-if-any-comm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!