What is the actual signal behind ERR

落花浮王杯 提交于 2019-12-12 18:28:13

问题


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:


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.


来源:https://stackoverflow.com/questions/26260581/what-is-the-actual-signal-behind-err

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!