0

Please consider:

scripttmp=$(mktemp -d)

cleanup() {
    rm -rf "${scripttmp}"
}

trap cleanup EXIT

I understand cleanup is a call(ed)back function, as it is being called just before exiting from the main function, from which it is part (I grasp the main function as a function in the general sense even though there is no function syntax around its code).

If I never actually called cleanup before --- I don't really "call it back"; I just call it before exiting, but not "back".

Is the term trap more accurate than the generic "callback" term in programming?

Osi
  • 1
  • 3
  • 9
  • 30

2 Answers2

4

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

Kusalananda
  • 14,885
  • 3
  • 41
  • 52
  • Hi Kusalananda. This might seem absurd to you but I am still stuck with the first example in the answer: `If I give someone my phone number and in case of need they call me back` **by** `phone number`, how can we compare it to a routine that "calls back per condition"; to what it calls back and what message it gives in that call? –  May 11 '19 at 18:01
  • 1
    @JohnDoea When using `trap action EXIT`, the trap is set for when the script terminates. There is no signal involved. However, using `trap action TERM` sets an action for when the `TERM` signal is received. `EXIT`, `RETURN`, `DEBUG` and `ERR` are special conditions that does not involve catching a signal. – Kusalananda Apr 14 '19 at 15:48
  • So I came out confuse from the answer and comments should read it again; I don't know what is the signal you refer too and that I hope to understand. –  Apr 14 '19 at 14:22
  • @JohnDoea Well, you have alternatives. I won't stop you from calling it a signal handler. Others would call it a callback (as there's no signal involved). – Kusalananda Apr 14 '19 at 12:37
  • I read the answer again and understand that what is done **is** handling a signal that says "about to exit if there isn't an handler in between", **then**, just before the exit **event**, `cleanup` is called. –  Apr 14 '19 at 12:20
  • Therefore I think "handling a signal" or "signal handler" is more intuitive or clear then "call(ed) back". –  Apr 14 '19 at 12:21
  • Would you say there could be a better term which won't have this possible confusion of call us again (via the phone) instead just "call us"? –  Apr 14 '19 at 11:57
  • @JohnDoea No, I would not say that. It's an accepted term for this sort of thing in programming in general. – Kusalananda Apr 14 '19 at 07:33
  • Thank you for the answer dear Kusalananda; would you say that indeed the term callback is problematic and confusing if we have to compare it to a phone call when someone calls us back via the phone?... –  Apr 14 '19 at 07:28
  • 1
    @JohnDoea I gave a number of alternatives in my answer: handler, signal handler, trap handler and trap. Out of these, the "trap" term is the most misleading, as it does not actually trap the signal (it is executed by the actual trap). – Kusalananda Apr 14 '19 at 12:04
  • Dear Kusalananda, in great plea - please review my comments to `user:EOhm` below; I don't want to clone them here. Thanks anyway for any of your precious time on this. –  Feb 05 '20 at 12:26
  • Hello; I have accepted your answer; you might want to include this link inside the answer: https://retrocomputing.stackexchange.com/questions/13479/what-was-the-first-introduction-of-callback-concept-of-programming –  Mar 29 '20 at 05:00
0

The term "call back" does not imply anyhow that it has to be called again (it's NOT called callagain or such).

It just means that the caller tells the callee how to notify him at some defined condition (so how to call him back then) - or which action to take at that condition (in behalf, the execution of the action goes back to the caller as the caller initiated that action through handing it over to the calle as the callback).

So the wording callback is very clear and consistent.

So for @Kusalananda example there are different possibilities.
Normally a single (or a single batch of) callback(s) is handed over to the callee (the routine, someone). It is well defined which type(s) of information(s) the callee will supply to the callback (so give it as arguments to the routine or tell it on phone to the recipient of the call). Possibly it's no information at all, just a call to the routine or a telephone call (so when the callback is defined to have no arguments or optional arguments). But anyhow normally the caller has some intention to hand the callback over to the callee, so the defined callback-routine or the one whos phone is ringing (it can also be a computer that gets the incoming call) is doing something the caller initiated when he gave that callback to the callee (so the action goes back to him, whether the target can identify the caller or not). Of course a callaback can be defined in a way that the callee that calls the callback always hands over some information to the callback-routine or to the someone answering the phone, it can be some detailed information about the condition, some information gathered by the calle in between or even a reference to the original caller.

On the other hand trap is a very specific word and not at all similiar or interchangeable with callback. It means about what the official translation/definition of the spoken word trap is. A trap is not called by the callee and is not handed over to a callee. A callee can be trapped. In Your example above the calee is just the remaining procedure following after installing the trap (up to the point where the trap is uninstalled or deactivated). A trap somehow is the opposite of a callback as the callee (or the trap insatlled by callee) traps some condition but from outside of the possible calees context.

apaderno
  • 28,547
  • 16
  • 75
  • 90
EOhm
  • 626
  • 4
  • 11
  • Well, I didn't understand the answer by Kusalananda (I usually always understand his, in my opinion, generally great answers and this is evident by the amount of answers of his I accepted and bountied in Unix and LInux SE). –  Feb 05 '20 at 12:24
  • I actually don't understand the term to this day humbly I am not desperate about it. Please review, for example: –  Feb 05 '20 at 12:24
  • https://cs.stackexchange.com/questions/119747/does-the-callback-concept-of-programming-has-any-basis-in-computer-science –  Feb 05 '20 at 12:24
  • https://retrocomputing.stackexchange.com/questions/13479/what-was-the-first-introduction-of-callback-concept-of-programming –  Feb 05 '20 at 12:24
  • You are most welcome to try to answer there ; perhaps it will help me to finally understand this term from these contextual and more basic questions. In appreciation - John, –  Feb 05 '20 at 12:25
  • @JohnDoea I do not think that I can or will answer in the terms You ask there. I'm not very interested in that and it is quite irrelevant. You did not mention the relevance of the question so the context or the scope is not defined. I think there are enough answers that describe what is meant by a callback and it is really easy to understand. So why asking this question that way? – EOhm Feb 18 '20 at 22:58
  • `Any time you can pass a procedure (function, method) designation (address, reference) as a parameter to a procedure, that is a "callback". ` –  Feb 19 '20 at 01:30
  • yes this is also a pretty good and quite generic definition @JohnDoea - but that does not mean that this is alway called callback. anyway it could be called like that if it acts as a callback tpye of procedure. (of course you could also pass this to a logging procedure which just logs informations about that and has no intention to execute, then it wouldn be a callback...) – EOhm Feb 19 '20 at 13:44