5

Pressing Ctrl-C while waiting for input from operation read -sp returns operation back to command line but input given is hidden like it is still running read -s.

example

#!/bin/sh

sig_handler() {
  echo "SIGINT received"
  exit 1
}

trap "sig_handler" SIGINT
read -sp "ENTER PASSWORD: " password
echo
echo $password

which executes normally like:

$~ ./example.sh
ENTER PASSWORD:
password
$~ text
-bash: text: command not found

but if you press Ctrl-C at ENTER PASSWORD you get

$~ ./example.sh
ENTER PASSWORD: SIGINT received
$~ -bash: text: command not found

where text or any other following command isn't displayed as input until you refresh with reset.

How can you return text to normal input after receiving SIGINT? read -p "ENTER PASSWORD: " password is not desired for obvious security reasons.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Mike
  • 53
  • 3

1 Answers1

4

Add stty sane to your signal handler so that it restores the terminal to its default state:

sig_handler() {
  echo "SIGINT received"
  stty sane
  exit 1
}
codeforester
  • 39,467
  • 16
  • 112
  • 140
  • That's interesting, I added stty sane and tput rs1 in the sig_handler before without any affect (testing now that works) must have accidentally put it below exit 1 or something. Thanks – Mike Feb 01 '17 at 18:05