错误 sh: 1: 读取: arg 计数

fin*_*oot 5 shell bash dash read

我只想等待用户通过按 确认消息Return。在 bash 中,我可以调用

$ read

$ 
Run Code Online (Sandbox Code Playgroud)

但是,在 sh (在我的情况下是破折号),我得到

$ read
sh: 1: read: arg count
$ 
Run Code Online (Sandbox Code Playgroud)

看来我必须提供一个论点?这种差异从何而来?

Kus*_*nda 10

标准的read实用需要至少一个变量的名称。

如果没有提供名称,某些 shell 的read实现会使用默认变量(例如REPLY)来存储读取的数据,但是dash旨在成为符合 POSIX 的 shell,则不会(因为标准不需要这样做)。dash壳中的等价物将是

read REPLY
Run Code Online (Sandbox Code Playgroud)

bash外壳,即使在其POSIX模式,保持功能启用一些非POSIX。这是其中之一,这意味着read即使您运行bash --posixshell ,没有变量名称也能工作。

有关启用 POSIX 模式时发生的事情的完整列表bash(这个问题实际上与此无关),请参阅https://www.gnu.org/software/bash/manual/html_node/Bash-POSIX-Mode。 html