"Callback" comes from the fact that you give a routine a piece of code for later execution (for whenever some condition is fulfilled), and the routine "calls back" by executing that code.
Compare this with giving someone your phone number for when they need it. When they need it, they call you back. At that point, they may never have called you before. The process of "calling" is the callback.
In a shell script, trap
is used to install a callback that will be evaluated when a signal is "caught" or "trapped". A standard shell also allows trapping a special event, namely when the shell exits (EXIT
), and the bash
shell additionally supports trapping errors (ERR
), exit from a function (RETURN
), and every simple command (DEBUG
).
The trap
utility does not allow for calling a callback function for generic asynchronous events.
In your example, cleanup
could be called a callback function. It is installed with trap
and will execute just before the current shell exits. In other words, the trap
utility installs an EXIT
trap that will call the callback function cleanup
when the EXIT
event is caught.
The code installed by trap action event
will be executed in a manner equivalent to eval action
when the given event
occurs. The action
could therefore be any shell code, not necessarily just a function call.
Another word for your cleanup
function would be a "handler", a routine that handles something (in this case, handling the termination of the script), and possibly more specifically "an EXIT
handler". If it was used to handle a caught signal, it would be "the signal handler for that particular signal". It is also common to call this function a "trap handler" (a handler installed by trap
) or just "trap", although this is not "more accurate".