Linux script to prompt for password

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

ADVERTISEMENTS

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.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
11 comments… add one
  • Dave Aug 17, 2006 @ 2:19

    Thanks!

  • Tukur Dan-Asabe Sep 12, 2006 @ 8:28

    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.

  • 🐧 nixCraft Sep 12, 2006 @ 14:13

    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/

  • Tukur Dan-Asabe Oct 19, 2006 @ 8:58

    Thanks nixcraft,

  • beta Mar 15, 2011 @ 4:48

    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

  • Josu May 27, 2011 @ 11:11

    Thank for this post, very interesting.

  • rem Nov 1, 2011 @ 15:55

    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

  • Mmm Jan 20, 2012 @ 4:37

    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?

    • balarajangeetha Oct 14, 2013 @ 1:57

      you may try the following (i am using them in SOLARIS)
      #
      echo “enter some_input”
      read some_input
      #
      # to display entered input and then to abort or proceed
      echo “entered some_input is $some_input. Press ENTER key to continue or CONTROL-C to abort”
      read
      #

  • Jacco Hoeve Feb 23, 2013 @ 17:39

    I took your script and made it fully automated:

    if [[ $(whoami) == 'root' ]]; then
      USER=johndoe
      read -s -p 'Enter Password:' PASSWORD
      HOSTNAME=10.0.1.16
      AFPSHARE="TM_MBP13"
      TM_NAME=$(hostname -s)
    
      # Don't change these ...
      MOUNT=/Volumes/TimeMachine
      SPARSEBUNDLE=$MOUNT/${TM_NAME}.sparsebundle
      PLIST=${SPARSEBUNDLE}/com.apple.TimeMachine.MachineID.plist
    
      echo "Disabling TimeMachine"
      tmutil disable
    
      echo "Unmounting ${AFPSHARE}"
      while mount | grep ${AFPSHARE} > /dev/null; do  mount | grep ${AFPSHARE} | awk '{print $3}' | xargs -n1 -I{} umount -f {}; done  
    
      echo "Mounting volume"
      mkdir $MOUNT
      mount_afp afp://${USER}:${PASSWORD}@${HOSTNAME}/${AFPSHARE} ${MOUNT}
    
      echo "Changing file and folder flags"
      chflags -R nouchg "${SPARSEBUNDLE}"
    
      echo "Attaching sparse bundle"
      DISK=$(hdiutil attach -nomount -readwrite -noverify -noautofsck "${SPARSEBUNDLE}" | grep 'Apple_HFS' | awk '{ print $1 }')  
    
      echo "Repairing volume"
      diskutil repairVolume ${DISK}
      /sbin/fsck_hfs -fry ${DISK}
    
      echo "Fixing Properties"
      cp "${PLIST}" "${PLIST}.backup"
      sed -e '/RecoveryBackupDeclinedDate/{N;d;}'   \
          -e '/VerificationState/{n;s/2/0/;}'       \
          "${PLIST}.backup" \
           > "${PLIST}"
    
      echo "Unmounting volumes"
      hdiutil detach ${DISK}
      umount ${MOUNT}
    
      echo "Enabling TimeMachine"
      tmutil enable
    
      echo "Starting backup"
      tmutil startbackup
      
    else
      echo Please run this script as root.
    fi
    
  • puppylpg Mar 19, 2016 @ 5:40

    Thanks a lot~

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.