Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zsh array of strings in for loop

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?

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

like image 934
Jikku Jose Avatar asked Jul 09 '15 09:07

Jikku Jose


1 Answers

The default form of the for command in zsh does not use parentheses (if there are any they are not interpreted as part of the for statement):

for string in some random strings to show
do
    echo _$string
done

This results in the following output:

_some
_random
_strings
_to
_show

So, echo _$string was run for each word after in. The list ends with the newline.

It is possible to write the whole statement in a single line:

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

As usual when putting multiple shell commands in the same line, newlines just need to be replaced by ;. The exception here is the newline after do; while zsh allows a ; to be placed after do it is usually not done and in bash it would be a syntax error.

There are also several short forms available for for, all of which are equivalent to the default form above and produce the same output:

  • 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 do or done:

    for string in some random strings to show ; echo _$string
    
  • without in but with parentheses, also without do or done

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

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

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

  • 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 zsh version).

like image 153
Adaephon Avatar answered Nov 16 '22 03:11

Adaephon