Differences between revisions 1 and 13 (spanning 12 versions)
Revision 1 as of 2010-08-18 16:20:29
Size: 1433
Editor: GreyCat
Comment: Why doesn't set -e (set -o errexit) do what I expected?
Revision 13 as of 2016-04-25 14:49:27
Size: 713
Editor: OrvilleMcq
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
<<Anchor(faq105)>>
== Why doesn't set -e (set -o errexit) do what I expected? ==

`set -e` was an attempt to add "automatic error detection" to the shell. Its ''goal'' was to cause the shell to abort any time an error occurred, so you don't have to put `|| exit 1` after each important command.

That goal is non-trivial, because many commands are ''supposed'' to return non-zero. For example,

{{{
  if [ -d /foo ]; then
    ...
  else
    ...
  fi
}}}

If the `[ -d /foo ]` command triggers the `set -e` abort when it returns non-zero (because the directory does not exist -- a case our script wants to handle in the `else` part), then obviously `set -e` isn't very useful. So the implementors decided to make a bunch of special rules, like "commands that are part of an `if` test are immune", or "commands in a pipeline, other than the last one, are immune".

These rules are extremely convoluted. Worse, they ''change'' from one Bash version to another, as Bash attempts to track the extremely slippery POSIX definition of this "feature". When a SubShell is involved, it gets worse still. The behavior changes depending on whether Bash is invoked in POSIX mode. [[http://fvue.nl/wiki/Bash:_Error_handling|Another wiki]] has a page that covers this in more detail. Be sure to check the caveats.

GreyCat's personal recommendation is simple: don't use it. Add your own explicit error checking instead.
Melvin Hartung is what my wife enjoys to call me although it is not the title on my birth certification. Tennessee is her birth place. I work as a inventory control and order filler but I've always wanted my own business. The favorite pastime for him and his children is to fence and he's been doing it for quite a whilst. Her spouse and her preserve a web site. You may want to check it out: http://www.netgestalter.de/blog/blogging/wieder-zurueck-und-das-ende-der-blogparade.html<br><br><<BR>>
<<BR>>
 http://i1038.photobucket.com/albums/a462/Harukomel/HTML/4.jpg Visit my blog post ... [[http://www.netgestalter.de/blog/blogging/wieder-zurueck-und-das-ende-der-blogparade.html|social media consulting]]<br>

Melvin Hartung is what my wife enjoys to call me although it is not the title on my birth certification. Tennessee is her birth place. I work as a inventory control and order filler but I've always wanted my own business. The favorite pastime for him and his children is to fence and he's been doing it for quite a whilst. Her spouse and her preserve a web site. You may want to check it out: http://www.netgestalter.de/blog/blogging/wieder-zurueck-und-das-ende-der-blogparade.html<br><br><<BR>>

BashFAQ/105 (last edited 2021-03-11 06:07:25 by dsl-66-36-156-249)