How do I echo stars (*) when reading password with `read`?
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/bashunset passwordprompt="Enter Password:"while IFS= read -p "$prompt" -r -s -n 1 chardo if [[ $char == $'\0' ]] then break fi prompt='*' password+="$char"doneechoecho "Done. Password=$password"
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/bashunset PASSWORDunset CHARCOUNTecho -n "Enter password: "stty -echoCHARCOUNT=0while IFS= read -p "$PROMPT" -r -s -n 1 CHARdo # 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" fidonestty echoecho $PASSWORD