如何撤消“set -e”的效果,如果任何命令失败,bash会立即退出?

问题描述 投票:0回答:4

在交互式 bash shell 中输入

set -e
后,如果任何命令以非零值退出,bash 将立即退出。我怎样才能取消这个效果?

bash exit
4个回答
372
投票

set +e
。是的,使用 set -
启用
shell 选项并使用 set +
禁用
它们是落后的。历史葡萄干,donchanow。


71
投票

每次想要覆盖它时都使用

set +e
/
set -e
可能不太方便。我找到了一个更简单的解决方案。

而不是这样做:

set +e
command_that_might_fail_but_we_want_to_ignore_it
set -e

你可以这样做:

command_that_might_fail_but_we_want_to_ignore_it || true

或者,如果您想节省击键并且不介意有点神秘:

command_that_might_fail_but_we_want_to_ignore_it || :

希望这有帮助!


21
投票
  • 使用 + 而不是 - 会导致这些标志被关闭。

来源


0
投票
(set +e; command_that_might_fail_but_we_want_to_continue_anyway)
© www.soinside.com 2019 - 2024. All rights reserved.