EN
Bash - read password in script without displaying characters
10 points
In this short article, we would like to show how to read password in Bash without displaying characters in the terminal.
Quick solution:
xxxxxxxxxx
1
2
3
read -s -p "Password: " password
Where:
-s
prevents against typed characters displaying,-p "Password: "
printsPassword:
message,password
is the name of the variable that will store the typed password.
Paste the following code into your bash script (e.g. password.sh
file).
xxxxxxxxxx
1
2
3
read -s -p "Password: " password
4
echo
5
6
if [ "$password" = 'secret_password' ]
7
then
8
echo "User authenticated...";
9
else
10
echo "Typed incorrect password!";
11
fi
12
13
read
Example result:
