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.

  • 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 3 '18 at 2:01

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.