Languages
[Edit]
EN

Bash - read password in script without displaying characters

10 points
Created by:
Wade
562

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.

Alternative titles

  1. Bash - disable echoing for typed password in shell script
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
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