Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I echo stars (*) when reading password with `read`?

What do I need to do for code in Bash, if I want to echo *s in place of password characters (or even just hide the characters completely) when the user types something in using read?

like image 643
Deniz Zoeteman Avatar asked Dec 17 '09 17:12

Deniz Zoeteman


People also ask

How do you put a password on a script?

#!/bin/bash echo "Enter Username : " # read username and echo username in terminal read username echo "Enter Password : " # password is read in silent mode i.e. it will # show nothing instead of password. read -s password echo echo "Your password is read in silent mode."

How do you echo a new line?

There are a couple of different ways we can print a newline character. The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way.

How do you read a shell script?

To illustrate how the command works, open your terminal, type read var1 var2 , and hit “Enter”. The command will wait for the user to enter the input. Type two words and press “Enter”. read and echo are enclosed in parentheses and executed in the same subshell.

Which of the following techniques is used by terminal driver to stop displaying the passwords on console in Linux?

The getpass() function first disables echoing and all processing of terminal special characters (such as the interrupt character, normally Control-C).


3 Answers

As Mark Rushakoff pointed out, read -s will suppress the echoing of characters typed at the prompt. You can make use of that feature as part of this script to echo asterisks for each character typed:

#!/bin/bash unset password prompt="Enter Password:" while IFS= read -p "$prompt" -r -s -n 1 char do     if [[ $char == $'\0' ]]     then         break     fi     prompt='*'     password+="$char" done echo echo "Done. Password=$password" 
like image 102
Dennis Williamson Avatar answered Sep 20 '22 10:09

Dennis Williamson


I really liked the answer that Wirone gave, but I didn't like that the backspacing would continue removing characters even back into the "Enter password: " prompt.

I also had some issues where pressing keys too rapidly would cause some of the characters to actually print on the screen... never a good thing when prompting for a password. =)

The following is my modified version of Wirone's answer which addresses these issues:

#!/bin/bash

unset PASSWORD
unset CHARCOUNT

echo -n "Enter password: "

stty -echo

CHARCOUNT=0
while IFS= read -p "$PROMPT" -r -s -n 1 CHAR
do
    # Enter - accept password
    if [[ $CHAR == $'\0' ]] ; then
        break
    fi
    # Backspace
    if [[ $CHAR == $'\177' ]] ; then
        if [ $CHARCOUNT -gt 0 ] ; then
            CHARCOUNT=$((CHARCOUNT-1))
            PROMPT=$'\b \b'
            PASSWORD="${PASSWORD%?}"
        else
            PROMPT=''
        fi
    else
        CHARCOUNT=$((CHARCOUNT+1))
        PROMPT='*'
        PASSWORD+="$CHAR"
    fi
done

stty echo

echo $PASSWORD
like image 34
Logan VanCuren Avatar answered Sep 20 '22 10:09

Logan VanCuren


read -s should put it in silent mode:

-s     Silent mode.  If input is coming from a terminal, characters are not echoed.

See the read section in man bash.

like image 44
Mark Rushakoff Avatar answered Sep 19 '22 10:09

Mark Rushakoff