Friday 19 May 2017

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?

Solution:

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

$ cat pass_script.sh

#!/bin/bash
echo -n "Enter username: "
read username
echo -n "Enter password: "
read -s passwd
echo
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 pass_script.sh
Enter username: root
Enter password: 
root, the password entered is hidden passwd

No comments:

Post a Comment