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.
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.
> 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.
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.
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)