zsh-workers
 help / color / mirror / code / Atom feed
From: "Lawrence Velázquez" <larryv@zsh.org>
To: "Daniil Iaitskov" <daniil.iaitskov@soostone.com>
Cc: zsh-workers@zsh.org
Subject: Re: bug: nested for loop body is executed once!
Date: Thu, 12 Aug 2021 23:50:34 -0400	[thread overview]
Message-ID: <34dbacca-01ee-4dda-a179-505c57760309@www.fastmail.com> (raw)
In-Reply-To: <CAAyLbqKCubQKvXMa5SRkQYgUYBLThzKujm43PmCAuJg45Psdqg@mail.gmail.com>

On Thu, Aug 12, 2021, at 11:13 PM, Daniil Iaitskov wrote:
> I just spot a following bug on Big Sur zsh 5.8 (x86_64-apple-darwin20.0)

It's not a bug.

> > $ K="1 2 3"
> > $ for i in $(for j in $K; do echo "ddd/$j" ; done) ; do echo  "$i" ; done
> > ddd/1
> > 2
> > 3
> 
> I would expect following output
> > ddd/1
> > ddd/2
> > ddd/3

By default, zsh does not word-split unquoted parameter expansions;
this behavior differs from most Bourne-adjacent shells.  Observe
that your "inner" loop actually only loops once, with $j taking on
the entire value of $K:

    % K="1 2 3"
    % for j in $K; do echo "<ddd/$j>"; done
    <ddd/1 2 3>

However, zsh *does* word-split unquoted command substitutions, so
the output of $(for j in $K; do echo "ddd/$j" ; done) is split into
'ddd/1', '2', and '3', and $i takes on each value in turn.

> > $ for i in $(for j in $(echo 1 2 3); do echo "ddd/$j" ; done) ; do echo  "$i" ; done
> 
> produce expected output:
> > ddd/1
> > ddd/2
> > ddd/3

In this example, $(echo 1 2 3) is word-split because it is a command
substitution.  Thus, $j takes on the values '1', '2', and '3', as
you expected.

> I don't know if this is a feature due to some legacy optimizations.
> I mostly use BASH and this behavior differs from BASH.
> BASH behaves exactly as I expect.

Actually, many zsh users consider the word-splitting of unquoted
parameter expansions to be a misfeature, which zsh's default behavior
remedies.  In any case, it's very much intentional.  You can obtain
word-splitting behavior with the ${=foo} form:

    % K="1 2 3"
    % for j in ${=K}; do echo "<ddd/$j>"; done
    <ddd/1>
    <ddd/2>
    <ddd/3>

You can also set SH_WORD_SPLIT, or rewrite the code to use an array.
If you're running code that relies on word-splitting (a POSIX script,
perhaps), you can run it under sh emulation.

> Wow! Are you still using just a mailing list for bug tracking?!

Yes.

-- 
vq


  parent reply	other threads:[~2021-08-13  3:51 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
2021-08-13  3:50 ` Lawrence Velázquez [this message]
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=34dbacca-01ee-4dda-a179-505c57760309@www.fastmail.com \
    --to=larryv@zsh.org \
    --cc=daniil.iaitskov@soostone.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).