tags:

views:

143

answers:

4

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.

+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
Thanks! Works fine!
YourComputerHelpZ
+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

Source: http://www.peterbe.com/plog/passwords-with-bash

Vinko Vrsalovic
Thanks! Works fine!
YourComputerHelpZ
+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
+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
This is what i really want! Thank you!
YourComputerHelpZ