问题
The bash builtin read
has a flag -s
that prevents it from echoing whatever is being read from the commandline. After searching opengroup.org and filtering through all the other meanings for read
, I still haven't found a POSIX/portable equivalent. Is there a reasonable way to do this?
In bash it's easy enough:
$ bash -c 'read -sp "What is your password? " password; printf "\n%s\n" "$password"'
What is your password?
I'll never tell!
But in sh…
$ dash -c 'printf "What is your password? "; read password >/dev/null 2>&1; printf "\n%s\n" "$password"'
What is your password? I'll never tell!
I'll never tell!
回答1:
So the answer to your question is as described in this link you can turn off by using builtin command stty
stty -echo
ps:
dont forget to save your previous settings
old_set=$(stty -g)
stty -echo
read -r password
stty "$old_set"
来源:https://stackoverflow.com/questions/25038184/can-you-portably-read-sensitive-input-from-the-commandline