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

BashExit

Bash Problem Overview


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?

Bash Solutions


Solution 1 - Bash

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

Solution 2 - Bash

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!

Solution 3 - Bash

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

Source

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTianyi CuiView Question on Stackoverflow
Solution 1 - BashzwolView Answer on Stackoverflow
Solution 2 - BashszeryfView Answer on Stackoverflow
Solution 3 - BashmhitzaView Answer on Stackoverflow