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.
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”.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With