2026年6月25日(木) 21:24 Léa Gris <[email protected]>:
> $ 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

I didn't carefully check `defer.c', but if the above is what is
achieved by the suggested loadable builtin, the builtin seems a very
thin wrapper of the trap builtin. The implementations by shell
functions posted by others are functionally equivalent and much
simpler than the C implementation with 195 lines.

The fork cost of subshells and the issue with the printf usage aren't
fundamental issues in implementing them with a shell function. As long
as a shell function is properly implemented, those issues shouldn't
arise. One can use no-fork command substitutions, as mentioned by
Oguz, and one can use «printf '%s\n%s' ...» or simply «"$1<newline>${
trap -P "$2"; }"», as suggested by Andreas and Gregory.

--
Koichi

Reply via email to