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?
Asked
Active
Viewed 6.3k times
237

Ciro Santilli OurBigBook.com
- 347,512
- 102
- 1,199
- 985

Tianyi Cui
- 3,933
- 3
- 21
- 18
3 Answers
367
With set +e
. Yeah, it's backward that you enable shell options with set -
and disable them with set +
. Historical raisins, donchanow.

zwol
- 135,547
- 38
- 252
- 361
-
2Thank you very much, it's among the very last lines of corresponding manual page (http://www.faqs.org/docs/bashman/bashref_56.html) which I didn't read to the end. – Tianyi Cui Aug 18 '10 at 22:22
-
The bash manual is dauntingly huge, it is true. (FYI, since you seem to be new: it is the done thing to click the check mark under the best answer to your question, this is called "accepting" it.) – zwol Aug 18 '10 at 22:26
-
And the format `set [--abefhkmnptuvxBCHP] [-o option] [argument ...]` is really misleading. I simply assumed the undo is done by another builtin, tried unset and got nothing. It's interesting that in bash `set` and `unset` are not opposite, while I admit I currently know little about bash programming. – Tianyi Cui Aug 18 '10 at 22:29
-
12Sadly, the Unix shell language (most of which is not specific to 'bash') is one of the least internally consistent programming languages still in wide use today. You're going to have to learn lots more of these little warts. And I'd say that's a documentation bug, there. – zwol Aug 18 '10 at 22:36
-
When I was about to report this documentation bug to GNU, I found that the bash manual hosted in faqs.org is last updated 13 November 2001... This bug is actually non-existent in current manual. But faqs.org's SEO seems better so I was mislead. I'll report it to faqs.org then. – Tianyi Cui Aug 20 '10 at 09:07
-
10historical grapes are raisin hell! – Tegra Detra Feb 06 '13 at 12:19
-
3Finally, an unfair bashing of Bash: single dash is the standard POSIX shell command line option, and therefore most natural for "do something". `+` is like `-` but crossing over something means "not" as in "≠". – Ciro Santilli OurBigBook.com Jun 29 '18 at 08:44
-
Bash is truly the worst – Andy Ray Jan 28 '20 at 18:41
69
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!

szeryf
- 3,197
- 3
- 27
- 28
-
11Was wondering about the history of `:`, and found my answer [here](http://stackoverflow.com/a/3224910/1094541), in case anyone else is curious. – 3cheesewheel Sep 23 '13 at 16:31
-
5This applies only if you don't care about the return code of the command you're running. – Isaac Oct 31 '17 at 11:32