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

ID : 131317

viewed : 11

Tags : bashshellpasswordsbash

Top 5 Answer for bash - How do I echo stars (*) when reading password with `read`?

vote vote

94

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" 
vote vote

89

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 
vote vote

77

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.

vote vote

66

I would like to add something to Dennis Williamson's solution:

#!/bin/bash  unset password echo -n "Enter password: " 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         prompt=$'\b \b'         password="${password%?}"     else         prompt='*'         password+="$char"     fi done 

In above example script handles backspace correctly.

Source

vote vote

60

I don't know about stars, but stty -echo is your friend:

 #!/bin/sh   read -p "Username: " uname   stty -echo   read -p "Password: " passw; echo   stty echo 

Source: http://www.peterbe.com/plog/passwords-with-bash

Top 3 video Explaining bash - How do I echo stars (*) when reading password with `read`?

Related QUESTION?