bash for 循环中的 Zsh 字符串数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31314187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:18:40  来源:igfitidea点击:

Zsh array of strings in for loop

bashcommand-linezsh

提问by Jikku Jose

Am trying to print a bunch of strings in a script (in zsh) and it doesn't seem to work. The code would work if I place the array in a variable and use it instead. Any ideas why this doesn't work otherwise?

我试图在脚本(在zsh)中打印一堆字符串,但它似乎不起作用。如果我将数组放在一个变量中并使用它,代码将起作用。任何想法为什么这不起作用?

for string in (some random strings to print) ; echo $string

for string in (some random strings to print) ; echo $string

回答by Adaephon

The default form of the forcommand in zshdoes not use parentheses (if there are any they are not interpreted as part of the forstatement):

zshfor命令的默认形式不使用括号(如果有括号,它们不会被解释为语句的一部分):for

for string in some random strings to print
do
    echo $string
done

There are also several short forms available for for:

还有几种简短的形式可用于for

  • for single commands (to be exact: single pipelines or multiple pipelines linked with &&or ||, where a pipeline can also be just a single command) there are two options:

    • the default form, just without door done:

      for string in some random strings to print ; echo string
      
    • without inbut with parentheses, also without door done

      for string (some random strings to print) ; echo $string
      
  • for a list of commands (like in the default form), foreachinstead of for, no in, with parentheses and terminated by end:

    foreach string (some random strings to print) echo $string ; end
    
  • 对于单个命令(确切地说:单个管道或多个与&&or链接||的管道,其中管道也可以只是一个命令)有两个选项:

    • 默认形式,只是没有dodone

      for string in some random strings to print ; echo string
      
    • 没有in但有括号,也没有dodone

      for string (some random strings to print) ; echo $string
      
  • 对于命令列表(如默认形式),foreach而不是for, no in,带括号并以 结束end

    foreach string (some random strings to print) echo $string ; end
    


In your case, you mixed the two short forms for single commands. Due to the presence of inzshdid not take the parentheses as syntactic element of the forcommand. Instead they interpreted as glob qualifier. Aside from the fact that you did not intend any filename expansions, this fails for two reasons:

在您的情况下,您混合了单个命令的两种简短形式。由于inzsh的存在,没有将括号作为for命令的句法元素。相反,它们被解释为 glob 限定符。除了您不打算进行任何文件名扩展这一事实之外,这失败的原因有两个:

  • there is no pattern (with or without actual globs) before the glob qualifier. So any matching filename would have to exactly match an empty string, which is just not possible

  • but mainly "some random strings to print" is not a valid glob qualifier. You probably get an error like "zsh: unknown file attribute: i" (at least with zsh 5.0.5, it may depend on the zshversion).

  • 在 glob 限定符之前没有模式(有或没有实际的 glob)。因此,任何匹配的文件名都必须与空字符串完全匹配,这是不可能的

  • 但主要是“要打印的一些随机字符串”不是有效的 glob 限定符。您可能会收到类似“zsh:未知文件属性:i”的错误(至少对于zsh 5.0.5,它可能取决于zsh版本)。

回答by Eugeniu Rosca

Check the zshforloop documentation:

检查zshfor循环文档

for x (1 2 3);  do echo $x; done
for x in 1 2 3; do echo $x; done

回答by Jahid

You are probably trying to do this:

您可能正在尝试这样做:

for string in some random strings to print ;do
   echo $string
done