我很确定答案是否定的,但我想我还是会问。我想要一个带有用户输入的 bash 脚本,该脚本在输入所需数量的字符后自动进行,在本例中为 3。
因此,当用户输入123or 时abc,脚本会继续使用该变量,而不是等待“Enter”。
read -p 'User Input: ' userInput
Run Code Online (Sandbox Code Playgroud)
这可能吗?
在 bash 中,您可以添加-n 3或-N 3取决于您需要的确切行为。来自help read:
Run Code Online (Sandbox Code Playgroud)-n nchars return after reading NCHARS characters rather than waiting for a newline, but honor a delimiter if fewer than NCHARS characters are read before the delimiter -N nchars return only after reading exactly NCHARS characters, unless EOF is encountered or read times out, ignoring any delimiter