Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Do you want to trap INT, or EXIT?


EXIT is trapped in the same way as 0, it's something that happens when your shell exits. Ctrl+C sends the SIGINT signal but you can catch it with INT too. You want to do the latter because your gracefully exiting the script, if you want to have some cleanup after that you could trap EXIT (for deleting tmp files or something).


Writing good SIGINT handler is a bit tricky. You should re-raise the signal, so that bash notices that the program was actually interrupted:

https://www.cons.org/cracauer/sigint.html (section "How to be a proper program")

Most of the time, especially for short lived script, adding signal handlers is not worth the trouble.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: