Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminal input hidden after interrupting 'read -s'

Tags:

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.

like image 580
Mike Avatar asked Feb 01 '17 17:02

Mike


1 Answers

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
}
like image 176
codeforester Avatar answered Sep 22 '22 11:09

codeforester