Le 25/06/2026 à 00:36, Gregory Heytings écrivait :

Is this better than

trap_append () { trap "$(printf "%s\n$1" "$(trap -P "$2")")" "$2"; }
trap_prepend () { trap "$(printf "$1\n%s" "$(trap -P "$2")")" "$2"; }

Well those are clever but fragile:

$ trap_prepend () { trap "$(printf "$1\n%s" "$(trap -P "$2")")" "$2"; }

$ trap_prepend 'echo "Hello World!"' INT
$ trap -p INT
trap -- 'echo "Hello World!"' SIGINT

$ trap_prepend 'rm -f -- "$tmp"; printf "Cleanup done on %s\\n" "$tmp"' INT

trap -p INT
rap -- 'rm -f -- "$tmp"; printf "Cleanup done on echo "Hello World!"\n" "$tmp"' SIGIN

This is not good!


Here with defer:

$ enable -n defer; enable -f ./defer defer
$ trap -- INT
$ defer 'echo "hello world"' INT
$ defer -p INT
trap -- 'echo "hello world"' SIGINT

$ defer 'rm -f -- "$tmp"; printf "Cleanup done on %s\\n" "$tmp"' INT
$ defer -p INT
trap -- 'rm -f -- "$tmp"; printf "Cleanup done on %s\\n" "$tmp"
echo "hello world"' SIGINT


--
Léa Gris

Reply via email to