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

Re: possible bug in zsh concerning a for loop



> 2022/03/18 16:33, Leander Jedamus <ljedamus@xxxxxxxxxxxxxx> 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.




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