I've read in several places (including SO) that -e
is considered "poor form" and is unreliable for exiting a script on any error. A better way to handle errors seems to be using trap
, as such:
trap "echo there was an error; exit 1;" ERR
I can't seem to locate in the man pages what signal ERR
is actually? I'm assuming it's SIGQUIT
but I can't find for sure.
man 7 signal
only has the normal signals you would expect SIGTERM SIGQUIT SIGINT
, etc.
man trap
has references to ERR
signal, but doesn't seem to define it.
ex: "A trap on ERR, if set, is executed before the shell exits.
"
man bash
is similar to man trap
in that is makes references to ERR
but doesn't define it from what I've seen.
What is the actual signal behind the shortcut ERR
? (in normal signals as seen in man 7 signal
).
I'd prefer to trap the actual signal name instead of the shorthand version, although I realize they would produce the same results (catching any error from a command in a script then throwing to the handler).
There is no signal corresponding to the
trap
signal specificationERR
.ERR
is one of the signal specifications implemented internally bybash
. [Note 1] Iftrap ERR
is enabled, then bash will invoke the corresponding handler in exactly the same cases as it would have exited hadset -e
been enabled. (Consequently, it is no more "reliable" thenset -e
but it is a lot more flexible.)Other special
trap
names which do not correspond to any signal areEXIT
,DEBUG
andRETURN
.help trap
will explain the meaning of these signal specifications.Notes:
bash
, but most of them are implemented bybash
trapping the signal and then executing the signal handler. The special ones just involve executing the signal handler.