* Unable to process a for loop as expected??
@ 1996-08-18 18:55 dpd
1996-08-18 18:06 ` Tom Kaczmarski
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: dpd @ 1996-08-18 18:55 UTC (permalink / raw)
To: zsh-workers
Hello there!
I hope I am writing to the proper person(s). If not just let me know
who to write to or better yet, forward this to the proper
party(ies).
My problem is trying to read in a string of values into a for loop.
A trivial script example is provided:
-------------------------------------------------------------
#!/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
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
Can some one help me resolve this issue. I am indeed looking for a
resolution that will allow me to produce the "expected" ouput in such
a for loop construct.
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?)
I am running the LINUX operating system, version 1.3.91.
If you need to know more info jusk ask :-).
Thanks for your help!
Dom.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Unable to process a for loop as expected??
1996-08-18 18:55 Unable to process a for loop as expected?? dpd
@ 1996-08-18 18:06 ` Tom Kaczmarski
1996-08-18 18:45 ` Zefram
1996-08-18 19:19 ` Richard Coleman
2 siblings, 0 replies; 4+ messages in thread
From: Tom Kaczmarski @ 1996-08-18 18:06 UTC (permalink / raw)
To: dpd; +Cc: zsh-workers
On Sun, 18 Aug 1996 dpd@asan.com wrote:
> -------------------------------------------------------------
> #!/bin/zsh
>
> list="a b c d"
>
> for character in $list
> do
> echo "Character is $character"
> done
> --------------------------------------------------------------
2 quick solutions
1) use "for character in a b c d e f g"
2) use "for character in `echo $list`
For some reason zsh seems to think that you're passing it only one
argument to the for statement.
Tom
?
''~``
( o o )
+----------------------------------.oooO--(_)--Oooo.------------------------+
mailto:tkaczma@luc.edu
http://orion.it.luc.edu/~tkaczma
.oooO (312) 512-7302
( ) Oooo.
+-------------------------------------\ (----( )--------------------------+
\_) ) /
(_/
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Unable to process a for loop as expected??
1996-08-18 18:55 Unable to process a for loop as expected?? dpd
1996-08-18 18:06 ` Tom Kaczmarski
@ 1996-08-18 18:45 ` Zefram
1996-08-18 19:19 ` Richard Coleman
2 siblings, 0 replies; 4+ messages in thread
From: Zefram @ 1996-08-18 18:45 UTC (permalink / raw)
To: dpd; +Cc: zsh-workers
>list="a b c d"
>for character in $list
You're trying to have word splitting done on $list, in order to
generate multiple words for the for loop. By default zsh does no field
splitting. There are three main approaches to this problem.
1. In order to get sh behaviour in this regard, use "setopt
sh_word_split". ("emulate sh" will do this, among other things, and
zsh will have this option set by default if invoked as sh.)
2. In order to do field splitting here only, leaving the default
behaviour unchanged, use $=list instead of $list.
3. In order to make the script use idiomatic zsh, rewrite it using an
array:
list=(a b c d)
for character in $list
This last method has the advantage that the values to be looped over
can contain whitespace:
list=(a b c 'd and some more text with spaces')
>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?)
For a while the man pages said 2.7 because that was expected to be the
next public release, and it would be too much trouble to change the man
pages every version. The latest release is 3.0.0, and the man pages
say 3.0.
>I am running the LINUX operating system, version 1.3.91.
Not a terribly stable version, AFAIK. Apparently 1.3.98 and 1.3.100
are much better. (I'm still running 1.3.80 -- very stable.)
-zefram
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Unable to process a for loop as expected??
1996-08-18 18:55 Unable to process a for loop as expected?? dpd
1996-08-18 18:06 ` Tom Kaczmarski
1996-08-18 18:45 ` Zefram
@ 1996-08-18 19:19 ` Richard Coleman
2 siblings, 0 replies; 4+ messages in thread
From: Richard Coleman @ 1996-08-18 19:19 UTC (permalink / raw)
To: dpd; +Cc: zsh-workers
> #!/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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~1996-08-18 19:20 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-08-18 18:55 Unable to process a for loop as expected?? dpd
1996-08-18 18:06 ` Tom Kaczmarski
1996-08-18 18:45 ` Zefram
1996-08-18 19:19 ` Richard Coleman
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).