help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Trick for saving and restoring $_ in trap handler.


From: Kaz Kylheku
Subject: Trick for saving and restoring $_ in trap handler.
Date: Tue, 09 Apr 2024 22:16:25 -0700
User-agent: Roundcube Webmail/1.4.15

Hi all, 

I have an interactive Bash environment in which a periodic
trap goes off for the ALRM signal.

It has come to my attention that this interferes with the
value of the $_ variable; it gets spontaneously clobbered.

You can't save it and restore it in the trap handler
function.

I worked out a trick of saving it in a global variable:

  trap 'uln_save=$_; handler; : "$uln_save"' ALRM

I.e. the trap isn't the function directly, but a three command
sequence. The first is an assignment that saves the current
value of $_ in $uln_save. Then we call the handler.
After that we execute a : null command, giving it "$uln_save"
as the argument. When that command is executed, that argument
will be stuffed into $_, and our job is done.

Cheers ...



reply via email to

[Prev in Thread] Current Thread [Next in Thread]