Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have noticed there are two alternative ways of building loops in zsh:

  1. for x (1 2 3); do echo $x; done
  2. 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 
share|improve this question
2  
for the example that "doesn't work", which is a csh style for loop, you're missing the parentheses. 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 4:22
    
It is SO crazy - that those two statements - indeed do not both "work the same". I literally can't get my head around it! I need to smoke some of what those shell designers were smoking', back in the day, lol. –  alex gray Apr 14 at 3:34
add comment

1 Answer

up vote 14 down vote accepted

Several forms of complex commands such as loops have alternate forms in zsh. These forms are mostly inspired by the C shell, which was fairly common when zsh was young but has now disappeared. These alternate forms act exactly like the normal forms, they're just a different syntax. They're slightly shorter, but less clear.

The standard form for the for command is for x in 1 2 3; do echo $x; done, and the standard form for the while command is while test …; do somecommand; done. Ksh, bash and zsh have an alternate form of for: for ((i = 0; i < 42; i++)); do somecommand; done, which mimics the for loops of languages like Pascal or C, to enumerate integers. Other exotic forms that exist in zsh are specific to zsh (but often inspired by csh).

share|improve this answer
    
Thanks a lot @Guilles. I am still a bit confused though. Which form is the specific one to zsh? Also, I have added one example at the end of my OP where I can't figure out how to translate between syntaxes. If the difference is just syntax, how should I fix the second script in the addendum to make it work? –  user815423426 Oct 24 '11 at 23:20
3  
@intrpc See my updated answer. As for your last example, it doesn't work because you wrote for x $d and zsh expects either in or ( where you wrote $d. Punctuation marks and reserved words can't come from a variable expansion, they have to be parsed before the shell can start on the variable expansions. –  Gilles Oct 24 '11 at 23:33
    
@intrpc both are zsh specific. zsh was specifically designed to support both bourne, korn, and c-shell syntax, as it's a hybrid of all three. –  Tim Kennedy Oct 25 '11 at 4:25
    
For more brevity in a common case, zsh also lets you omit do ... done for a single command: for i in 1 2 3; echo $i. –  Ulrich Schwarz Oct 25 '11 at 9:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.