Hello, what do i need to do for code in Bash, if i want to put stars, or even just that you cant see anything, when the user types something in using read
. What do i need to do to edit read
, so it can have like stars or so.
views:
143answers:
4
+1
A:
stty -echo
read something
stty echo
will stop user input being echoed to the screen for that read. Depending on what you are doing with prompts, you may want to add an extra echo
command to generate a newline after the read.
moonshadow
2009-12-17 17:54:37
Thanks! Works fine!
YourComputerHelpZ
2009-12-17 18:02:14
+3
A:
I don't know about stars, but stty -echo is your friend:
#!/bin/sh
read -p "Username: " uname
stty -echo
read -p "Password: " passw; echo
stty echo
Vinko Vrsalovic
2009-12-17 17:55:02
+4
A:
read -s
should put it in silent mode:
-s Silent mode. If input is coming from a terminal, characters are not echoed.
See the read
section in man bash
.
Mark Rushakoff
2009-12-17 17:56:06
+3
A:
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/bash
unset password
prompt="Enter Password:"
while read -p "$prompt" -s -n 1 char
do
if [[ $char == $'\0' ]]
then
break
fi
prompt='*'
password+="$char"
done
echo
echo "Done. Password=$password"
Dennis Williamson
2009-12-17 19:06:46