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

后端 未结 3 810
逝去的感伤
逝去的感伤 2021-01-30 01:12

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?

3条回答
  •  清酒与你
    2021-01-30 01:41

    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!

提交回复
热议问题