bash builtin read
有一个标志-s
,可以阻止它回显从命令行读取的内容。在searching opengroup.org之后过滤read
的所有其他含义后,我仍然找不到POSIX /便携式等价物。有合理的方法吗?
在bash中它很容易:
$ bash -c 'read -sp "What is your password? " password; printf "\n%s\n" "$password"'
What is your password?
I'll never tell!
但在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!
答案 0 :(得分:3)
所以问题的答案如下link所述 您可以使用内置命令stty
关闭stty -echo
PS:
别忘了保存以前的设置
old_set=$(stty -g)
stty -echo
read -r password
stty "$old_set"