How can I use the backspace character as a backspace when entering a password?

6,550

Try this:

#!/bin/bash
unset password
prompt="Enter Password:"
while IFS= read -p "$prompt" -r -s -n 1 char 
do
    if [[ $char == $'\0' ]];     then
        break
    fi
    if [[ $char == $'\177' ]];  then
        prompt=$'\b \b'
        password="${password%?}"
    else
        prompt='*'
        password+="$char"
    fi
done
echo " "
echo "Done. Password=$password" 

The options of the read command are:
-p : Prompt string.
-r : Don't use backslash as escape character.
-s : Silent mode, inputs are not echoed.
-n 1 : Number of character to input.

read returns 0 unless \0 is encountered, and the character the user types is placed into the char variable.

The IFS= part clears the IFS variable, which ensures that any space or tab characters that you type are included in the password rather than being parsed out by read.

Share:
6,550

Related videos on Youtube

user158335
Author by

user158335

Updated on September 18, 2022

Comments

  • user158335
    user158335 over 1 year

    I just saw this code somewhere.
    It allows the user to enter his password and change it to an asterisk/star.
    But it doesn't recognize the backspace key.
    The code thinks that the backspace is part of the password.
    What should I do to make it recognize the backspace?
    And can someone explain this code to me?
    Especially the IFS, the options of read, and the $'\0'
    Thanks!

    Here is the code.

    \#!/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"
    
    • Clive van Hilten
      Clive van Hilten almost 11 years
      Rather than mess with the usual method of changing your password and risk being locked out, just use the standard passwd command in a terminal session. In any event, when you enter your password the characters are not echoed to screen for security reasons, so the script you've copied in your question accomplishes nothing extra.
    • user158335
      user158335 almost 11 years
      I'm actually writing a program that creates a user account. So I need this script as a part of my program.
    • user68186
      user68186 almost 11 years
      This looks like an homework assignment for a course. Flagging it as Too Localized.
    • Eric Carvalho
      Eric Carvalho almost 11 years
      I think unix.stackexchange.com is a better place to ask this.
  • user158335
    user158335 almost 11 years
    Thanks for explaining! But unfortunately, the code still doesn't work. What's the use of \177?
  • Dipto
    Dipto almost 11 years
    @user158335, 177 is the value of backspace in octal. $\177 is used to check the input is a backspace or not. This code is perfectly working in my system. what is the problem you are facing?
  • user158335
    user158335 almost 11 years
    It's okay now! Haha. I was running my previous program instead of yours. Thanks again!!