How do I set a function to run on exit in fish shell?

Viewed 171

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.

1 Answers
Related