【问题标题】:Bash for loop without arguments [duplicate]没有参数的Bash for循环[重复]
【发布时间】:2020-06-30 22:16:07
【问题描述】:

有人可以澄清一下为什么下面的代码在 bash 中是有效的,如果有的话,效果是什么?

for value ; do
  echo $value
done

据我所知,bash 只是忽略了这段代码,但我不明白为什么它不会引发语法错误或其他什么?

【问题讨论】:

  • 在您的终端中运行 help for 并尝试使用 shellcheck.net 验证 shell 脚本。
  • 谢谢!您能否将此添加为答案,以便我接受?
  • 没关系,我就留在这里吧。

标签: bash


【解决方案1】:

正如@Jetchisel 指出的,我们可以在shell 中运行help for 来查看for 命令的文档字符串。

$ help for
for: for NAME [in WORDS ... ] ; do COMMANDS; done
    Execute commands for each member in a list.
    
    The `for' loop executes a sequence of commands for each member in a
    list of items.  If `in WORDS ...;' is not present, then `in "$@"' is
    assumed.  For each element in WORDS, NAME is set to that element, and
    the COMMANDS are executed.
    
    Exit Status:
    Returns the status of the last command executed.

我不知道的部分在下面突出显示以强调:

[...] 如果 `in WORDS ...;'不存在,则假定为 `in "$@"'。 [...]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 2013-08-24
    • 2011-06-13
    • 2013-07-19
    • 2018-02-14
    相关资源
    最近更新 更多