Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

exit

People also ask

How do you exit a script if command fails?

Exit When Any Command Fails This can actually be done with a single line using the set builtin command with the -e option. Putting this at the top of a bash script will cause the script to exit if any commands return a non-zero exit code.

Which command causes a bash script to stop running and exit the shell?

One of the many known methods to exit a bash script while writing is the simple shortcut key, i.e., “Ctrl+X”. While at run time, you can exit the code using “Ctrl+Z”.

How do I exit bash script without exiting shell?

If you are executing a Bash script in your terminal and need to stop it before it exits on its own, you can use the Ctrl + C combination on your keyboard. A ^C character will appear in your terminal to indicate a keyboard interrupt.

How do you remove a Pipefail?

So set +e will undo set -e , and set +o pipefail will undo set -o pipefail .


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


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!


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

Source