bash内置的read
有一个标志-s
,可以防止它回显从命令行读取的任何内容。在搜索opengroup.org并通过read
的所有其他含义过滤后,我仍然没有找到POSIX/可移植的等效物。有什么合理的方法可以做到这一点吗?
在bash中这很简单:
$ bash -c 'read -sp "What is your password? " password; printf "n%sn" "$password"'
What is your password?
I'll never tell!
$ dash -c 'printf "What is your password? "; read password >/dev/null 2>&1; printf "n%sn" "$password"'
What is your password? I'll never tell!
I'll never tell!
那么你的问题的答案就在这个链接中您可以使用内置命令stty
关闭。stty -echo
ps: 不要忘记保存你以前的设置
old_set=$(stty -g)
stty -echo
read -r password
stty "$old_set"