Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

3
  • Note that in shells like bash, if you intercept SIGINT and exit normally in a child process, then the script won't exit upon ^C (same for SIGQUIT vs ^\ ). To work around that, you'd need to replace that return with a kill -s INT <mypid> (where <mypid> is the pid of the subshell (but beware not all shells run subshells in a child process)). Commented Jan 30, 2021 at 7:35
  • As you're reading the key from the controlling tty (/dev/tty), you may want to issue the prompt there as well instead of stdout which may be redirected (or maybe stderr). Commented Jan 30, 2021 at 7:43
  • @StéphaneChazelas it's even more broken than that. a) it doesn't handle TSTP/CONT b) it calls stty "$t" twice c) it doesn't work in yash. I've tried to do something that work in as many shells as possible (thence the awkward return), but the results are still less than stellar ;-) A version which do WCE correctly but only work in bash would not catch the INT, QUIT and TERM at all, but rely on bash running the EXIT trap even when terminating because of a signal. Commented Jan 31, 2021 at 22:19