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

后端 未结 10 837
死守一世寂寞
死守一世寂寞 2020-11-29 02:13

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 so

10条回答
  •  -上瘾入骨i
    2020-11-29 02:38

    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
    

提交回复
热议问题