zsh-workers
 help / color / mirror / code / Atom feed
From: Matthew Martin <phy1729@gmail.com>
To: zsh-workers@zsh.org
Subject: Re: bug: nested for loop body is executed once!
Date: Thu, 12 Aug 2021 22:47:28 -0500	[thread overview]
Message-ID: <YRXrUK3gO19NCt7O@CptOrmolo.darkstar> (raw)
In-Reply-To: <CAAyLbqKCubQKvXMa5SRkQYgUYBLThzKujm43PmCAuJg45Psdqg@mail.gmail.com>

On Thu, Aug 12, 2021 at 11:13:09PM -0400, Daniil Iaitskov wrote:
> Hi,
> 
> I just spot a following bug on Big Sur zsh 5.8 (x86_64-apple-darwin20.0)
> 
> $ K="1 2 3"
> > $ for i in $(for j in $K; do echo "ddd/$j" ; done) ; do echo  "$i" ; done
> > ddd/1
> > 2
> > 3

That's because zsh doesn't do word splitting on parameter expansions by
default. Running under set -x is illuminating.

+zsh:3> j=1 2 3
+zsh:3> echo 'ddd/1 2 3'
+zsh:3> i=ddd/1
+zsh:3> echo ddd/1
ddd/1
+zsh:3> i=2
+zsh:3> echo 2
2
+zsh:3> i=3
+zsh:3> echo 3
3

j is assigned '1 2 3' and the result of the command substitution is
'ddd/1 2 3'. However word splitting does happen on unquoted command
substitutions, so i is assigned to thrice.

> I would expect following output
> 
> > ddd/1
> > ddd/2
> > ddd/3

You want an array: K=(1 2 3)

At which point you could skip the command substitution and instead
for i in ddd/$^K; do ...


  reply	other threads:[~2021-08-13  3:47 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-13  3:13 Daniil Iaitskov
2021-08-13  3:47 ` Matthew Martin [this message]
2021-08-13  3:50 ` Lawrence Velázquez
2021-08-14 15:07   ` Daniel Shahaf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YRXrUK3gO19NCt7O@CptOrmolo.darkstar \
    --to=phy1729@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).