What is the actual signal behind ERR

2019-07-15 10:09发布

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).

1条回答
不美不萌又怎样
2楼-- · 2019-07-15 10:33

There is no signal corresponding to the trap signal specification ERR.

ERR is one of the signal specifications implemented internally by bash. [Note 1] If trap ERR is enabled, then bash will invoke the corresponding handler in exactly the same cases as it would have exited had set -e been enabled. (Consequently, it is no more "reliable" then set -e but it is a lot more flexible.)

Other special trap names which do not correspond to any signal are EXIT, DEBUG and RETURN.

help trap will explain the meaning of these signal specifications.


Notes:

  1. Actually, all of the signal specifications are implemented by bash, but most of them are implemented by bash trapping the signal and then executing the signal handler. The special ones just involve executing the signal handler.
查看更多
登录 后发表回答