help-bash
[Top][All Lists]
Advanced

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

Re: A typo in Bash manual and how to have a "local" trap instruction


From: Laurent Lyaudet
Subject: Re: A typo in Bash manual and how to have a "local" trap instruction
Date: Fri, 20 Mar 2026 11:16:30 +0100

Hello Martin :),

Le ven. 20 mars 2026 à 01:31, Martin D Kealey
<martin@kurahaupo.gen.nz> a écrit :
>
> Salut Laurent
>
>
>
> On Fri, 20 Mar 2026, 01:02 Laurent Lyaudet, <laurent.lyaudet@gmail.com> wrote:
>>
>> cloning of bash repository is still ongoing,
>
>
> Yeah, git clone from from Savannah can sometimes be frustratingly slow.
>
> If it helps, I maintain a mirror of the Savannah bash repo on GitHub, at 
> https://github.com/kurahaupo/bash
>
> (It is updated whenever I work on bash, usually at least once a week, which 
> should be enough to base a patch on.)
>
> To create a new local repo:
> git clone git+ssh://github.com/kurahaupo/bash.git
> or
> git clone https://github.com/kurahaupo/bash.git
>
> or inside an existing local repo
>
> git remote add kurahaupo git+ssh://github.com/kurahaupo/bash.git
> git fetch kurahaupo
> git stash # if you have already made changes
> git checkout devel
> git merge --fastforward kurahaupo/devel
> git branch your-branch-name
> git stash pop # recover your changes if any
>
>> How to have something like a "local" trap
>> instruction on RETURN sigspec ?
>
>
>> Right now, I'm thinking about having a single trapped command on RETURN that 
>> would be in my code, and that function would handle a
>> stack of trap instructions for functions,
>> and it would do the work of popping each instruction, executing it, etc.
>
>
> That is broadly workable, and less messy than you might think.
>
> Debugging traps including RETURN execute in the context of the current 
> function, so you could simply use
>
>  local ReturnTrapHandler='My Command':${#FUNCNAME[@]}
>
> and then have your return traps look at that variable:
>
>  case $ReturnTrapHandler in
>   *:${#FUNCNAME[@]})
>     eval "${MyReturnTrapHandler%:*}"
>  esac
>
> Tracking the calling stack depth via  ${#FUNCNAME[@]} ensures that this only 
> fires in functions that define this variable, not in their subfunctions, even 
> if you have recursive functions or functions that do not define the variable.
>
> (If your trap handler is itself a function then you'll need to adjust this 
> logic accordingly.)
>
> -Martin

Thanks for the git commands but I already know them,
apart from the --fast-forward that I never use.
I checked my local clone just now:
$ grep -rn 'signal handling' . | grep 'builtin modifies the shell'
grep: ./doc/bash.info : fichiers binaires correspondent
./doc/bashref.texi:3953:The @code{trap} builtin modifies the shell's
signal handling, as
./doc/bashref.html:4459:<p>The <code class="code">trap</code> builtin
modifies the shell&rsquo;s signal handling, as
./doc/bash.info:3256:   The ‘trap’ builtin modifies the shell's signal
handling, as described
grep: ./doc/bashref.info : fichiers binaires correspondent
./doc/bash.0:3281:       The trap builtin modifies the shell's signal
handling, as described be-
./doc/bash.1:5974:The \fBtrap\fP builtin modifies the shell's signal
handling, as
./doc/bashref.info:3257:   The ‘trap’ builtin modifies the shell's
signal handling, as described
./doc/bash.html:7501:The <B>trap</B> builtin modifies the shell's
signal handling, as

As you can see, there are 7 files impacted,
and unfortunately, I don't know much about standard documentation in
source repositories.
What is the "master doc file" ?
Is it the .texi ? Or the .info ?
And then a command compiles the other files from it ?
The doc/README file doesn't explain it.
It just says that the .info is compiled from the .texi by makeinfo.
So I assume that I should modify directly all files except for the .info.
I do know slightly better the .rst files in Python ecosystem, but
without being an expert.
I will provide a patch later today,
since I will be busy soon.

Thanks for the trick of ${#FUNCNAME[@]}.
I didn't know how to handle recursive functions.
I need to do something slightly modified,
because I want that
local ReturnTrapHandler
is an array to have many commands executed on one RETURN
(some pushd needs a popd,
and some shopt -s needs a shopt -u, etc.,
all these requirements are handled separately,
hence the need for many commands executed on one RETURN
unless you do less factorized code with a custom wrapper per function
instead of more factorized code with "atomic" functions.
("atomic" in terms of features semantic, or SRP))

Best regards,
    Laurent Lyaudet



reply via email to

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