How to hide text input in terminal
Solution 1:
You may add -s
to your read
command (you are not providing it, so I suppose you are using read -r
), so it would become:
read -r -s password
From read --help
:
-r do not allow backslashes to escape any characters -s do not echo input coming from a terminal
Solution 2:
In order for the terminal not to show typed text, echo
needs to be turned off. The command stty -echo
does this. To return to normal, run stty echo
.
Example:
#!/bin/sh
printf "Enter password: "
stty -echo
read -r password
stty echo
echo ""
echo "password=$password"
If, due to a typo or other error, the script fails to restore echo, run reset
to recover.
The above script, including the use of stty and read, is POSIX compatible. This means it will work with both bash
and /bin/sh
.