Linux script to prompt for password

by Vivek Gite on February 14, 2006 · 8 comments

Linux do not provide any special or specific command to read password. However, bash shell comes with builtin command called read. It read date from the standard input (keyboard), or from file descriptor FD if the -u option is supplied. General syntax is as follows:
read -s -p "Password: " VARIABLE

Where,

  • -s: Do not display password on screen. It causes input coming from a terminal/keyboard to not be echoed
  • -p: "Password: ": Display text message
  • VARIABLE: Bash variable in which your password stored

For example, try out following example at shell prompt:
$ read -s -p "Enter Password: " mypasswordOutput:

Enter Password:  

Now display password:
$ echo $mypassword See complete script to accept password using read command.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 8 comments… read them below or add one }

1 Dave August 17, 2006

Thanks!

Reply

2 Tukur Dan-Asabe September 12, 2006

Please I want to y1, y2 and y3 to have values piped in by the pipe (|) and read to read from the file and asign them to the variables but

export tukur=2
test is a file contain 3 columns say: aa bb cc
after excuting the following command y1, y2, y3 dont contain anythin pls could someone help?

tail -n +$tukur home/tukur/test | read -r y1 y2 y3

Thank you,
Tukur Dan-Asabe.

Reply

3 nixcraft September 12, 2006

Tukur,

Assuming that your file.txt is as follows:
val1 val2 val3

Here is a shell script:

#!/bin/sh
while read line
do
# store field 1
F1=$(echo $line|cut -d” ” -f1)
# store field 2
F2=$(echo $line|cut -d” ” -f2)
# store field
F3=$(echo $line|cut -d” ” -f3)
echo $F1 ” ” $F2 ” ” $F3
done < file.txt

Var $F1 $F2 and $F3 stores the value. If you need more help try out shell scripting forum @ http://forum.cyberciti.biz/

Reply

4 Tukur Dan-Asabe October 19, 2006

Thanks nixcraft,

Reply

5 beta March 15, 2011

Unfortunately, read -s only works if the first line of your script sets the shell to bash. Tested in ubuntu.

IF THIS does work
#!/bin/sh

set to
#!/bin/bash

Reply

6 Josu May 27, 2011

Thank for this post, very interesting.

Reply

7 rem November 1, 2011

Hi Vivek,

How to make a one liner that can implement the prompt script to run multiple times?
eg. the simplest one i can think of:

i have one user list, name it as userlist.txt with content as below:
mary:mary@mail.com
kent:kent@mail.com
judy:judy@mail.com
……

with a script that used to reset single user password, resetpwd.sh
#!/bin/bash

user=$1

read -p “reset user password (y/n)” yn

case $yn in
y|Y)
echo “plsresetlater” | passwd –stdin $user &> /dev/null;;
n|N)
echo “user password did not change”;;
esac

then run the one liner by piping all the username to the resetpwd.sh; see output:

reset user password (y/n) y #user mary
reset user password (y/n) n #user kent
user password did not change
reset user password (y/n) n #user judy
user password did not change
……

i have tried the awk; not working
awk -F “:” ‘{print $1}’ userlist.txt | while read users; do resetpwd.sh $users; done

Reply

8 Mmm January 20, 2012

hi guys,
if in the middle of execution of script ,
some process asked for some input from keyboard . How can i provide that input from the script?

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 3 + 3 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.



Previous post:

Next post: