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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With