在交互式 bash shell 中输入
set -e
后,如果任何命令以非零值退出,bash 将立即退出。我怎样才能取消这个效果?
与
set +e
。是的,使用 set -
启用shell 选项并使用
set +
禁用它们是落后的。历史葡萄干,donchanow。
每次想要覆盖它时都使用
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 || :
希望这有帮助!
- 使用 + 而不是 - 会导致这些标志被关闭。
(set +e; command_that_might_fail_but_we_want_to_continue_anyway)