zsh-users
 help / color / mirror / code / Atom feed
* possible bug in zsh concerning a for loop
@ 2022-03-18  7:33 Leander Jedamus
  2022-03-18  8:45 ` Jun T
  0 siblings, 1 reply; 5+ messages in thread
From: Leander Jedamus @ 2022-03-18  7:33 UTC (permalink / raw)
  To: Zsh Users

Hi!

I tried to get a normal for loop to work with zsh. Now I think I found a 
bug.

A short script is
----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<
#!/bin/zsh

items="item1 item2 item3"

for i in $items; do
   echo "\"$i\""
done
----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<

This prints:
"item1 item2 item3"

It should print:
"item1"
"item2"
"item3"

I hope that somebody can verify this.

Thanks in advance
Leander Jedamus
-- 
.o.
_^_ Ich bin nicht das Problem. Ich bin die Lösung.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: possible bug in zsh concerning a for loop
  2022-03-18  7:33 possible bug in zsh concerning a for loop Leander Jedamus
@ 2022-03-18  8:45 ` Jun T
  2022-03-18 14:14   ` Vincent Lefevre
  0 siblings, 1 reply; 5+ messages in thread
From: Jun T @ 2022-03-18  8:45 UTC (permalink / raw)
  To: zsh-users; +Cc: Leander Jedamus


> 2022/03/18 16:33, Leander Jedamus <ljedamus@googlemail.com> wrote:
> 
> items="item1 item2 item3"
> 
> for i in $items; do
>  echo "\"$i\""
> done

> This prints:
> "item1 item2 item3"

This is not a bug but a feature of zsh.
Yes, it is incompatible with other shells (sh/ksh/bash ...),
and one of the most significant incompatibilities.

See 'man zshexpn' and search for SH_WORD_SPLIT.

> It should print:
> "item1"
> "item2"
> "item3"

You can get this by:

[1] use array (instead of scalar parameter items)

a=( item1 item2 item3 )
for i in $a; do ... done

[2] use ${=items}

for i in ${=items}; do ... done

[3] set SH_WORD_SPLIT option

setopt SH_WORD_SPLIT
for i in $items; do ... done

I prefer [1], but it is up to you which method you use.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: possible bug in zsh concerning a for loop
  2022-03-18  8:45 ` Jun T
@ 2022-03-18 14:14   ` Vincent Lefevre
  0 siblings, 0 replies; 5+ messages in thread
From: Vincent Lefevre @ 2022-03-18 14:14 UTC (permalink / raw)
  To: Jun T; +Cc: zsh-users, Leander Jedamus

On 2022-03-18 17:45:22 +0900, Jun T wrote:
> You can get this by:
> 
> [1] use array (instead of scalar parameter items)
> 
> a=( item1 item2 item3 )
> for i in $a; do ... done
> 
> [2] use ${=items}
> 
> for i in ${=items}; do ... done
> 
> [3] set SH_WORD_SPLIT option
> 
> setopt SH_WORD_SPLIT
> for i in $items; do ... done
> 
> I prefer [1], but it is up to you which method you use.

[1] has the advantage that it supports arguments containing spaces.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: possible bug in zsh concerning a for loop
@ 2022-03-18 10:12 Leander Jedamus
  0 siblings, 0 replies; 5+ messages in thread
From: Leander Jedamus @ 2022-03-18 10:12 UTC (permalink / raw)
  To: Zsh Users

Thanks Jun T.
${=items} solved it for me. When I used to set the setopt-variable, it
just messed my system...

Thanks
Leander Jedamus
--
.o.
_^_ Ich bin nicht das Problem. Ich bin die Lösung.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* possible bug in zsh concerning a for loop
@ 2022-03-18  8:21 Leander Jedamus
  0 siblings, 0 replies; 5+ messages in thread
From: Leander Jedamus @ 2022-03-18  8:21 UTC (permalink / raw)
  To: Zsh Users

Hi!

I tried to get a normal for loop to work with zsh. Now I think I found a
bug (zsh is 5.7.1).

A short script is
----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<
#!/bin/zsh

items="item1 item2 item3"

for i in $items; do
   echo "\"$i\""
done
----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<

This prints:
"item1 item2 item3"

It should print:
"item1"
"item2"
"item3"

I hope that somebody can verify this.

Thanks in advance
Leander Jedamus
--
.o.
_^_ Ich bin nicht das Problem. Ich bin die Lösung.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-03-18 14:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-18  7:33 possible bug in zsh concerning a for loop Leander Jedamus
2022-03-18  8:45 ` Jun T
2022-03-18 14:14   ` Vincent Lefevre
2022-03-18  8:21 Leander Jedamus
2022-03-18 10:12 Leander Jedamus

Code repositories for project(s) associated with this 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).