Read -s with timeout 在超时后显示文本

Read -s with timeout shows text after timeout

你好,我正在使用 read in bash 请求密码,并使用 -s 隐藏输入和 -t 10 超时,除一件事外,所有工作都按预期进行。
这是代码(我正在使用数组开关):

read -t 10 -a mp -s -p "Enter Password:"

它隐藏输入,如果我不输入任何内容,它 returns 会在 10 秒内提示。
但是,如果我输入 12 个字符传递的前 10 个字符,并且当它 returns 时超时,它会显示我在下一行输入的字符。

示例:

DD-WRT-Bash:~# read -t 10 -a mp -s -p "Enter Password:"

(我输入 testing123)

如果我没有按回车键并且超时,下一行是:

DD-WRT-Bash:~# testing123

它在 Ubuntu 中做同样的事情。
有没有办法防止文本在下一行返回?

我建议使用 -N 选项来规避 read 仅消耗整行输入的行为。

-N的描述开头如下:

-N nchars
read returns after reading exactly nchars characters
rather than waiting for a complete line of input, unless
EOF is encountered or read times out.

使用一个足够大的值,使您的用户在超时之前无法访问它似乎效果很好:

read -t 10 -a mp -s -N 9999 -p "Enter Password:"

部分行将存储到数组中,不会在控制台的下一行输入中重现。