Languages
[Edit]
EN

Bash - read password in script without displaying characters

10 points
Created by:
Wade
352

In this short article, we would like to show how to read password in Bash without displaying characters in the terminal.

Quick solution:

#!/bin/bash

read -s -p "Password: " password

Where:

  • -s prevents against typed characters displaying,
  • -p "Password: " prints Password:  message,
  • password is the name of the variable that will store the typed password.

 

Practical example

Paste the following code into your bash script (e.g. password.sh file).

#!/bin/bash

read -s -p "Password: " password
echo

if [ "$password" = 'secret_password' ]
then
	echo "User authenticated...";
else
	echo "Typed incorrect password!";
fi

read

Example result:

Reading password in Bash script without displaying characters.
Reading password in Bash script without displaying characters.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join