help-bash
[Top][All Lists]
Advanced

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

Re: Trick for saving and restoring $_ in trap handler.


From: alex xmb sw ratchev
Subject: Re: Trick for saving and restoring $_ in trap handler.
Date: Wed, 10 Apr 2024 09:36:14 +0200

to reuse $_ u must save it , normal practice

On Wed, Apr 10, 2024, 07:17 Kaz Kylheku <kaz@kylheku.com> wrote:

> 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]