Skip to main content
8 events
when toggle format what by license comment
Nov 7, 2020 at 9:45 comment added Stéphane Chazelas @FranklinYu, functions bodies are evaluated by the same shell process. So unless the function bodies is a subshell (as in func() (echo in a subshell), If you kill the process that runs the function, you kill the script. That kill vs exit only matters when the waiting process behaves like bash (zsh doesn't). Inside your own script, you can do what you want based on the exit status of your function.
Nov 7, 2020 at 8:11 comment added Franklin Yu Thanks for the complete guideline. Does the “kill better than exit” rule also applies to functions? In other words, is there “kill better than return”?
Nov 7, 2020 at 6:45 comment added Stéphane Chazelas @FranklinYu, 130 is the number most shells (including zsh) put in $? when the child process they're waiting for dies of a SIGINT, but doing exit 130 is not the same thing as being killed by SIGINT. See Default exit code when process is terminated? for details. And see How to trigger error using Trap command and unix.stackexchange.com/a/230568 for why it may be preferable to kill yourself with SIGINT (like I hint in the answer) than to do exit 130 if you want to report to your parent that you've been interrupted.
Nov 7, 2020 at 5:09 comment added Franklin Yu Sorry, the first time I read this, I somehow missed the localtraps line. Reading it again, there is nothing to “clean up”. This is perfect solution! Thank you. One nitpick: I prefer to preserve the original return value for INT, which is 130.
Oct 19, 2020 at 8:11 history edited Stéphane Chazelas CC BY-SA 4.0
added 48 characters in body
Oct 19, 2020 at 6:07 vote accept Franklin Yu
Oct 19, 2020 at 6:07 comment added Franklin Yu It’s sad to hear that I still need to trap the INT, because I actually don’t care how the function is terminated. I’m going to simply document the assumption that INT trap was unset before calling the function; this assumption makes my clean-up part way easier (otherwise I would need to go this far). Thanks anyway!
Oct 18, 2020 at 7:12 history answered Stéphane Chazelas CC BY-SA 4.0