Reputation: 85
You can see what problem I'm having just by doing sleep 10
and typing.
How would I go about stopping that typing from showing?
Upvotes: 3
Views: 1586
Reputation: 19555
You could do this like that:
#!/usr/bin/env bash
exit_trap ()
{
# Purge pending typed data if any
# While there is data to read
while read -rt0
do IFS= read -r # read the data
done < /dev/tty # from the tty terminal
# Restore terminal echo
stty echo
}
# If running from a terminal
if [ -t 1 ]
then
# Program exit trap to cleanup and restore terminal echo on
trap 'exit_trap' EXIT
# Disable terminal echo
stty -echo
fi
echo "going to sleep 5"
sleep 5
echo "finished sleeping"
Upvotes: 1
Reputation: 94644
To turn off keyboard echo, you can use stty -echo
. To turn it back on you can do stty echo
. To be safe, you would save the stty state and restore it at the end; however if you're reading a password, bash has a built-in -s
option to read.
In summary:
stty -echo
# keyboard input is not echoed
stty echo
# keyboard input is echoed
More complete:
state=$(stty -g)
stty -echo
# keyboard is not echoed
stty "$state"
# state is restored.
Better, if you're just asking for a password, and using bash:
read -s password
# password is now in the variable $password; no stty shenanigans
Upvotes: 3