2

I have this bash-written hangman game for a school project, and currently I am trying to replace the ctrl-c exit command for a while loop break, so the user goes back from the actual game to the categories selection menu.

The code I am currently using is:

trap "break" SIGINT

and inside the while loop:

read CHAR

Everything works fine when a text is printed in the loop and also when the sleep command is running, but when the algorithm is at the read command, pressing ctrl-c just inputs "^C" on the console.

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
  • Please update your post to show all the steps you have taken. Include as much of the script as possible to help troubleshoot the issue. – kemotep Apr 03 '18 at 02:01

1 Answers1

3

If bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes.

That is the reason why the read is not interrupted.

The ^C is not printed by the shell but by the terminal due to the setting echoctl. If you execute stty -echoctl then it does not appear.

Hauke Laging
  • 88,146
  • 18
  • 125
  • 174