I want to do some cleanup before my shell script exits, including when it's cancelled with Ctrl-C.
I tried this code (in a script.fish
file):
function exit_handler --on-event fish_exit
echo exiting!
end
sleep 5 &
sleep 4 &
wait
If I let this code run normally, the exit_handler
function is called as expected, but if I interrupt it with Ctrl-C, exiting!
is not printed. Is there some way to always run the event handler, even if the script is aborted?
I'm using fish 3.4.0 on macOS 12.3.
It's not yet possible to trap control-C (SIGINT) in non-interactive scripts. This is a known limitation. See https://github.com/fish-shell/fish-shell/issues/6649