Re: A defer command as a bash loadable builtin
Gregory Heytings <[email protected]> Fri, 26 Jun 2026 16:29:24 +0000
| Newsgroups | gmane.comp.shells.bash.bugs |
|---|---|
| Message-ID | <[email protected]> |
>
> I'm not sure if it is robust to concatenating the trap strings by a
> newline. When one of the registered trap strings contains a syntax
> error, it would affect the subsequent commands. When one of the trap
> strings contains the builtin changing the control flow, such as
> `return', `break', and `continue`, subsequent registered trap strings
> would be skipped. I think the proper implementation should manage an
> array for each signal, which is separate from the store managed by the
> trap builtin.
>
Something like
trap_store=()
trap_eval() { for ((i=0;i<${#trap_store[@]};i+=2)); do [[ "${trap_store[i+1]}" == "$1" ]] && (eval "${trap_store[i]}"); done; }
trap () { local CMD="$1"; shift; while (($#>0)); do trap_store+=("$CMD" "$1"); builtin trap "trap_eval $1" "$1"; shift; done; }
?