I'm writing a Bourne shell script and have a password input like this:
echo -n 'Password: '
read password
Obviously, I don't want the password being echoed to the terminal, so I want to turn off echo for the duration of the read. I know there's way to do this with stty
, but I'll ask the question for the benefit of the community whilst I go read the manpage. ;)
Writing Text to the Terminal To write a simple string of text to the terminal window, type echo and the string you want it to display: echo My name is Dave. The text is repeated for us.
In computing, echo is a command that outputs the strings that are passed to it as arguments. It is a command available in various operating system shells and typically used in shell scripts and batch files to output status text to the screen or a computer file, or as a source part of a pipeline.
This prints the specified text string before producing a listing of all the files in the current working directory, across the screen. echo recognizes a number of escape sequences which it expands internally. An escape command is a backslash-escaped character that signifies some other character.
The echo command repeats typed text back to the screen and can send text to a peripheral on the computer, such as a COM port.
stty_orig=`stty -g`
stty -echo
echo 'hidden section'
stty $stty_orig
read -s password
works on my linux box.
You can use '-s' option of read command to hide user input.
echo -n "Password:"
read -s password
if [ $password != "..." ]
then
exit 1; # exit as password mismatched #
fi
Also you can use 'stty -echo' if you want to hide from terminal to print. And restore the terminal settings using "stty echo"
But I think for getting password input from user 'read -s password' is more than enough.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With