Thursday, 25 April 2013

Read password without showing / displaying on the screen

Q) My requirement is to write a bash script which prompts for the username, Password and then do some operations. When the password is entered on the unix terminal / screen, the password should not be displayed on the terminal. How to do this?


The following unix or linux shell script reads the username, password from the terminal and then prints the values on the output screen:
$ cat

echo -n "Enter username: "
read username
echo -n "Enter password: "
read -s passwd
echo "$username, the password entered is $passwd"
#Statements to connect to remote box
#using the entered username and password.

The -s option to the read command makes the value to be not printed on the screen.
# bash
Enter username: root
Enter password: 
root, the password entered is hidden passwd

0 blogger-disqus:

Post a Comment