From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 725 invoked by alias); 3 Nov 2015 07:48:06 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 20888 Received: (qmail 26419 invoked from network); 3 Nov 2015 07:48:05 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, FREEMAIL_REPLY,HTML_MESSAGE,T_DKIM_INVALID autolearn=no autolearn_force=no version=3.4.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=N/CLMKJPdmL1vCQeEPm8WBBiTHAHYnXt2s/PhFbF55c=; b=WNLCF+3PACO/KKncUoyPZgRD1gJYR/+l6loWkXm7hDzXfjDRlC73jMoMgr/zp9x0oH lxE3ldp+DgG3iHl0gBwc9znbhGsa0LHd9DmBs7innP6tRGFaDBibQhlj9nFVaUtx5u5d vhlVlWvr4y6av7rSsgaoSBOidowD/d6R1BR8kq2ApZQgTYkTtVeIv1WUlQ/F0R8kgvb0 ZqPlWBelRF3cKfnwop/mAbEe0HgJ/vG/jouXwkGtKEBb+lbir8ctgtyWLcyXjP5ZZTSL WjzeCv4hFRwzaL6lvov4AAt5RHIUchFvXj/F17sTkcuG3McUFpvmgyJ15VHLogw0NE8Y qHQA== X-Received: by 10.202.60.214 with SMTP id j205mr4370000oia.99.1446536881968; Mon, 02 Nov 2015 23:48:01 -0800 (PST) MIME-Version: 1.0 Sender: a.skwar@gmail.com In-Reply-To: <20151103073547.GA6580@chaz.gmail.com> References: <20151103073547.GA6580@chaz.gmail.com> From: Alexander Skwar Date: Tue, 3 Nov 2015 08:47:42 +0100 X-Google-Sender-Auth: ZduyFm5O5QrkMAj6SfOvyknXFvo Message-ID: Subject: Re: for loop with parameter expansion in zsh vs. bash To: zsh-users@zsh.org Content-Type: multipart/alternative; boundary=001a113cd388011d8905239e1d35 --001a113cd388011d8905239e1d35 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hello 2015-11-03 8:35 GMT+01:00 Stephane Chazelas : > 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/sbi= n:/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. > =E2=80=8BWell, but I do know that my variable does not contain blanks or wi= ldcards. As I said, PATH was just an example, so that everybody can easily follow. So, no, =E2=80=8Bthe approach was not at all wrong. Your assumption was, th= ough. > > 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=3D: # split on : > set -f # disabe glob > for e in $PATH > =E2=80=8BThanks a lot. Works great.=E2=80=8B > > 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:) > =E2=80=8BGood catch ;)=E2=80=8B > > > > > > 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-f= orgetting-to-quote-a-variable-in-bash-posix-shells > ) > > In zsh, you invoke the split ($=3Dvar) or glob ($~var) operators > explicitely, so: > > IFS=3D: > for e in $~PATH > > Or use the "s" expansion flag: > > for e in "${(s(:))PATH}" > > =E2=80=8BGreat. 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.=E2=80=8B > For PATH however, you don't need to as zsh ties the $PATH > variable to the $path array. > =E2=80=8BI shouldn't have used PATH as an example=E2=80=A6 :/=E2=80=8B But = thanks for the heads up for that as well. Alexander --=20 =3D> *Google+* =3D> http://plus.skwar.me <=3D=3D =3D> *Chat* (Jabber/Google Talk) =3D> a.skwar@gmail.com <=3D=3D --001a113cd388011d8905239e1d35--