zshell for statement

zshell for statement

am 15.02.2006 11:21:22 von Simon

In ksh on Solaris I can do:

a="hello goodbye seeya sayonara"

for word in $a
do
echo "$word"
done

and the output is:

hello
goodbye
seeya
sayonara

In zsh on Solaris the script returns
hello goodbye seeya sayonara

With zsh if I substitute "hello goodbye seeya soyonara" for $a it still
doesn't work.
If I do this:
for word in hello goodbye seeya sayonara

then I get the correct output from zsh.

Anyone know why zsh does not appear to be properly handling the $a or
the "word1 word2" string with the for statement?

Re: zshell for statement

am 15.02.2006 11:30:43 von Erik Max Francis

Simon wrote:

> With zsh if I substitute "hello goodbye seeya soyonara" for $a it still
> doesn't work.
> If I do this:
> for word in hello goodbye seeya sayonara
>
> then I get the correct output from zsh.
>
> Anyone know why zsh does not appear to be properly handling the $a or
> the "word1 word2" string with the for statement?

In zsh it should be:

a=(hello goodbye seeya sayonara)

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Don't fire unless fired upon. But if they want a war let it begin
here. -- Cpt. John Parker

Re: zshell for statement

am 15.02.2006 11:59:48 von Stephane CHAZELAS

On 15 Feb 2006 02:21:22 -0800, Simon wrote:
> In ksh on Solaris I can do:
>
> a="hello goodbye seeya sayonara"

That's a string (scalar) variable.

> for word in $a
> do
> echo "$word"
> done
>
> and the output is:
>
> hello
> goodbye
> seeya
> sayonara

Do you find it normal? Your looping over one string variable,
and get 4 passes in the loop.

do you expect;

for word in "hello goodbye seeya sayonara"
do
echo "$word"
done

to behave that way as well.

> In zsh on Solaris the script returns
> hello goodbye seeya sayonara

That makes more sense to me.

> With zsh if I substitute "hello goodbye seeya soyonara" for $a it still
> doesn't work.
> If I do this:
> for word in hello goodbye seeya sayonara
>
> then I get the correct output from zsh.

Yes, there, you're asking it to loop over 4 words.

> Anyone know why zsh does not appear to be properly handling the $a or
> the "word1 word2" string with the for statement?

Because it fixed that bug you encountered in the other shells.

If you want zsh to split a string variable into several words as
other shells do implicitely, on need to ask it to do it:

for word in $=var

(see $= as scissors) (note that it uses $IFS just as the other
shells, that is not, as you seem to think, that
for word in $var
is the same as
for word in the expansion of that variable
(fortunately))

That's not the whole story, the other shells are even more bogus
than that. Not only do they split the string variables by
default, but they also do globbing (expand the *, ?... that are
contained in the variable). If you want that as well with zsh,
then it's:

for word in $=~var

Note that "emulate sh" or "emulate ksh" will make zsh emulate
those bogus behaviors.

Obviously, above, you want $a to be a list, so why do you make
it a string variable?

a=(hello goodbye seeya sayonara)

will make it a list variable. And to loop over the values:

for i in "$a[@]"

You can also do

for i in $a

But zsh will then discard the empty elements (
a=(a '' b)
for i in $a
print -r -- $i
done

will output
a
b
)

--
Stephane

Re: zshell for statement

am 15.02.2006 12:13:05 von Simon

Erik and Stephane,
Thanks for the answers!
Simon