Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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"
share|improve this question

closed as too localized by user68186, Eric Carvalho, Thomas W., RolandiXor May 24 '13 at 19:31

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
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. –  Clive van Hilten May 24 '13 at 10:29
    
I'm actually writing a program that creates a user account. So I need this script as a part of my program. –  user158335 May 24 '13 at 11:02
    
This looks like an homework assignment for a course. Flagging it as Too Localized. –  user68186 May 24 '13 at 12:24
    
I think unix.stackexchange.com is a better place to ask this. –  Eric Carvalho May 24 '13 at 13:43

1 Answer 1

up vote 2 down vote accepted

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|improve this answer
    
Thanks for explaining! But unfortunately, the code still doesn't work. What's the use of \177? –  user158335 May 24 '13 at 14:20
    
@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? –  Dipto May 25 '13 at 18:21
    
It's okay now! Haha. I was running my previous program instead of yours. Thanks again!! –  user158335 May 28 '13 at 7:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.