Bash builtin read command doesn't seem to support it, now I need to let user input a password while no echo should be displayed, what tool can I use?
Asked
Active
Viewed 1,418 times
14
-
4`help read|grep echo`: "`-s` do not echo input coming from a terminal" – donothingsuccessfully Nov 16 '12 at 07:25
-
1@donothingsuccessfully This `-s` extension of `read` is [not standard](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/read.html). You may not be able to use it on any Unix/Linux. – Totor Apr 22 '13 at 21:26
1 Answers
23
#!/bin/bash
stty -echo
IFS= read -p 'Enter password: ' -r password
stty echo
printf '\nPassword entered: %s\n' "$password"
stty -echoturns off the terminal echo, which is the display you're talking about;IFS=is necessary to preserve whitespace in the password;read -rturns off backslash interpretation.
In bash you can also use read -s, but this feature isn't standard across shells.
Chris Down
- 122,090
- 24
- 265
- 262