Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Unable to process a for loop as expected??



> #!/bin/zsh
> 
> list="a b c d"
> 
> for character in $list
> do
>    echo "Character is $character"
> done
>  
> The expected output (Exactly what I wish to accomplish):
> 
> Character is a
> Character is b
> Character is c
> Character is d
> 
> Actual output (Not what I want to accomplish):
> 
> Character is a b c d

This is a feature and not a bug.  It is discussed in the FAQ.
If you want to split the word, you can either turn on the option
shwordsplit which will make zsh work like other shells with
respect to word splitting, or you can use

list="a b c d"
for character in ${=list}
do
    echo "Character is $character"
done

which tells zsh to split the variable when it is evaluated.
With zsh, you get to choose whether to split the variable, rather
than it happening by default.

> A similar script using the /bin/bash shell produces the "expected" output, 
> however, using the /bin/zsh shell produces the "actual", i.e., 
> unwanted, output
> 
> BTW, according to the extracted sources, I am using zsh version zsh-2.6-beta19.  
> However, according to the man pages, I am using zsh version 2.7.  (Why 
> the discrepancy?)

Initially, zsh 2.6 was the beta version for 2.7.  Since there was plenty
of changes, the major version has now been moved to 3.  I suggest you
upgrade to 3.0.

rc



Messages sorted by: Reverse Date, Date, Thread, Author