I have noticed there are two alternative ways of building loops in zsh:
for x (1 2 3); do echo $x; done
for x in 1 2 3; do echo $x; done
They both print:
1
2
3
My question is, why the two syntaxes? Is $x
iterating through a different type of object in each of them?
Does bash make a similar distinction?
Addendum:
Why does the following work?:
#!/bin/zsh
a=1
b=2
c=5
d=(a b c)
for x in $d; do print $x;done
but this one doesn't?:
#!/bin/zsh
a=1
b=2
c=5
d=(a b c)
It complains with "parse error near `$d'"
for x $d; do print $x;done
for x ($d); do print $x; done
will work, and it will match the first syntax that you have enumerated at the beginning of your question. – Tim Kennedy Oct 25 '11 at 04:22for i ({0..4..2}) for j ({a..c}) echo "($i,$j)"
={0,2,4}x{a,b,c}
. Semicolons apply to the outermost loop and redirections apply to the innermost, and if you need to change that, you only need braces:for i ({0..4..2}) { for j ({a..c}) echo "($i,$j)" } | cat -n
={1,...,9}*({0,2,4}x{a,b,c})
. Of course you can combine loops with zsh expansion:for i ("("{0..4..2}","{a..c}")") echo $i
– John P Jun 08 '18 at 04:45