Can you portably read sensitive input from the commandline?

扶醉桌前 提交于 2021-02-04 19:40:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!