Hi,
I have to read the password input and display it as * in the console
How can I do this??
Hi,
I have to read the password input and display it as * in the console
How can I do this??
This works for ksh93 and should be easily modifable for bash.
passwd=""
omodes=`stty -g`
echo "Enter Password: \c"
while :
do
stty raw
c=$(dd bs=1 count=1 2>/dev/null)
stty -raw
# break out of loop if CR found
[[ -z $(echo $c | tr -d "\015") ]] && break
stty echo
echo "*\c"
passwd=${passwd}${c}
stty -echo
done
stty $omodes
echo
echo "Password entered: $passwd"
For bash, the only change needed is to explicitly tell the build-in echo command to interpret "\c" correctly i.e.
passwd=""
omodes=`stty -g`
echo -e "Enter Password: \c"
while :
do
stty raw
c=$(dd bs=1 count=1 2>/dev/null)
stty -raw
# break out of loop if CR found
[[ -z $(echo $c | tr -d "\015") ]] && break
stty echo
echo -e "*\c"
passwd=${passwd}${c}
stty -echo
done
stty $omodes
echo
echo "Password entered: $passwd"
I love the idiosyncrasies of the echo command!
if your looking for a simple solution to just hide the password, and you're using bash, you could just use "read -s" ...
#!/bin/bash
echo -n "Enter a Password: "
read -s pass_value
echo ""
echo "Password Entered: $pass_value"
Thanks for all the replies.
In bash when i type the password it prints the first character on screen
and then types * correctly....but the input is correct
How can i overcome this??