zsh-users
 help / color / mirror / code / Atom feed
From: Alexander Skwar <alexanders.mailinglists+nospam@gmail.com>
To: zsh-users@zsh.org
Subject: Re: for loop with parameter expansion in zsh vs. bash
Date: Tue, 3 Nov 2015 08:47:42 +0100	[thread overview]
Message-ID: <CADn-QaNAt3y2qd4dyBnhd9ifdMOqf9DsVwFbVfdriap6uohvRA@mail.gmail.com> (raw)
In-Reply-To: <20151103073547.GA6580@chaz.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3704 bytes --]

Hello

2015-11-03 8:35 GMT+01:00 Stephane Chazelas <stephane.chazelas@gmail.com>:

> 2015-11-03 07:52:51 +0100, Alexander Skwar:
> > Hello
> >
> > I've got a variable, where seperate values are limited with a delimiter.
> > Let's say PATH with : (but the question is general).
> >
> > With bash, I can easily create a for loop which loops over all the
> > elements, when I have bash replace the ":" with a " ", like so:
> ${PATH//:/
> > }.
> >
> > a@ubuntu-notebook:~$ echo $PATH
> >
> /home/a/Applications/go/bin:/home/a/bin:/opt/bin:/opt/sbin:/usr/local/sbin:/usr/local/bin:/home/a/Applications/copy/x86_64:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/a/Applications/adt-bundle-linux-x86_64/sdk/platform-tools:/home/a/Applications/btsync:/home/a/.rvm/bin
> >
> > a@ubuntu-notebook:~$ for e in ${PATH//:/ }; do echo e: $e; done
> > e: /home/a/Applications/go/bin
> > e: /home/a/bin
> > e: /opt/bin
> > e: /opt/sbin
> > e: /usr/local/sbin
> > e: /usr/local/bin
> > e: /home/a/Applications/copy/x86_64
> > e: /usr/sbin
> > e: /usr/bin
> > e: /sbin
> > e: /bin
> > e: /usr/games
> > e: /usr/local/games
> > e: /home/a/Applications/adt-bundle-linux-x86_64/sdk/platform-tools
> > e: /home/a/Applications/btsync
> > e: /home/a/.rvm/bin
>
> That approach is wrong. You're replacing : with space and
> invoking the split+glob operator (leaving an expansion unquoted
> in ksh/bash/sh, not zsh) with the default value of IFS which
> contains space, tab and newline.
>
> That means that approach only works if $PATH components don't
> contain blanks or wildcards.
>

​Well, but I do know that my variable does not contain blanks or wildcards.
As I said, PATH was just an example, so that everybody can easily follow.
So, no, ​the approach was not at all wrong. Your assumption was, though.



>
> Here, you could use the split+glob operator, but you want to
> split on :, not blanks, and not invoke the glob part.
>
> So it would be (bash/ksh/sh, not zsh unless in sh/ksh emulation):
>
> IFS=: # split on :
> set -f # disabe glob
> for e in $PATH
>

​Thanks a lot. Works great.​



>
> However with bash/ksh/sh (not zsh), it's still wrong, because it
> would discard a trailing empty component (like for a $PATH value
> of /bin:/usr/bin:)
>


​Good catch ;)​



>
>
> >
> > In zsh, the same for loop does not work (like it does in bash):
>
> No, because and that's the most FAQ for zsh, leaving a variable
> unquoted is not the split+glob operator (like it is in most
> other shells and the number one source of bugs and security
> vulnerabilities in them,
> (
> http://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells
> )
>
> In zsh, you invoke the split ($=var) or glob ($~var) operators
> explicitely, so:
>
> IFS=:
> for e in $~PATH
>
> Or use the "s" expansion flag:
>
> for e in "${(s(:))PATH}"
>
>
​Great. Thanks. Appreciated.


Too bad, that there's such a difference between the shells. Makes it hard
to share snippets with co-workers, who (for reasons, that I fail to
understand) don't use zsh.

Because of that, I'm actually using something along the lines of:

  for e in $(echo "$PATH" | tr ':' ' '); do echo e $e; done

This works everywhere.​



> For PATH however, you don't need to as zsh ties the $PATH
> variable to the $path array.
>

​I shouldn't have used PATH as an example… :/​ But thanks for the heads up
for that as well.


Alexander
-- 
=>        *Google+* => http://plus.skwar.me         <==
=> *Chat* (Jabber/Google Talk) => a.skwar@gmail.com <==

  reply	other threads:[~2015-11-03  7:48 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CADn-QaM6ZEP32i+6t_DC4bA8iJF_VR5hVsP6bMyxqMATxazVCw__45247.5188104019$1446533685$gmane$org@mail.gmail.com>
2015-11-03  7:35 ` Stephane Chazelas
2015-11-03  7:47   ` Alexander Skwar [this message]
     [not found]   ` <CADn-QaNAt3y2qd4dyBnhd9ifdMOqf9DsVwFbVfdriap6uohvRA__32237.289628438$1446536964$gmane$org@mail.gmail.com>
2015-11-03  8:47     ` Stephane Chazelas
2015-11-03  6:52 Alexander Skwar
2015-11-03  7:13 ` lilydjwg
2015-11-03  7:49   ` Alexander Skwar

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=CADn-QaNAt3y2qd4dyBnhd9ifdMOqf9DsVwFbVfdriap6uohvRA@mail.gmail.com \
    --to=alexanders.mailinglists+nospam@gmail.com \
    --cc=zsh-users@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).