About Linux FAQ

Browse More FAQs:

Linux script to prompt for password

Posted by Vivek Gite [Last updated: September 1, 2006]

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.

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Other Helpful FAQs:

Discussion on This FAQ

  1. Dave Says:

    Thanks!

  2. Tukur Dan-Asabe Says:

    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.

  3. nixcraft Says:

    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/

  4. Tukur Dan-Asabe Says:

    Thanks nixcraft,

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.