From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8012 invoked by alias); 3 Nov 2015 07:13:53 -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: 20887 Received: (qmail 6944 invoked from network); 3 Nov 2015 07:13:52 -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=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to :disposition-notification-to:user-agent; bh=DLMIy7WpHc9ee2bjzb9/SME5lGZCP/I4HpBaa1lGhM4=; b=ZrQzbFRzy3KyGvEuLu9LW6Y31EFgJWpxBsbRiD2HTkPIDroK70dmsyuYzB2Xhr0QQc DVg+Iys5DQXOf8BUTGpVpRP8NDdgExzMwgo2zAD6eXmJWPb2O9hZWjdRxYm68dBc4SGn RZBrISqB4sUv0F3eyMotlCDtwUQ7oCwQCMwtUsRsszDBqhqYEyG3DiXJoD/h4Lgt6njx ci3gdmESqiEZx9AkM75BDsYhGDKcXUSIwOS0qA7ygmc9uBpV3yJ8DU9CqUgu4nOJSzo4 4cSu8+wXGuVAfDhY2yOvUC6SG4SHqLKKSjYDdKCp9GaFaA5XyC+xZI60NtS9VZFUWstD VaXQ== X-Received: by 10.68.174.1 with SMTP id bo1mr31646748pbc.110.1446534829462; Mon, 02 Nov 2015 23:13:49 -0800 (PST) Date: Tue, 3 Nov 2015 15:13:12 +0800 From: lilydjwg To: zsh-users@zsh.org Subject: Re: for loop with parameter expansion in zsh vs. bash Message-ID: <20151103071312.GB13762@lilyforest> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) On Tue, Nov 03, 2015 at 07:52:51AM +0100, Alexander Skwar wrote: > 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 > > > In zsh, the same for loop does not work (like it does in bash): > > 7:51% for e in ${PATH//:/ }; do echo e: $e; done > e: /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 > > > As you can see there, zsh had only 1 iteration of the for loop. > > What would be the zsh way to loop over elements, which are delimited by a > ":" (or "," or ";" or whatever)? > > Thanks a lot, % for e in ${=PATH//:/ }; do echo e: $e; done zsh has sh_word_split option unset by default, so use ${=var} to request word splitting explicitely. I like this behaviour very much, because I don't need to quote nearly every variable :-) PS: there is a coresponding $path array, and you can tie your own variables like this: export -TU PYTHONPATH pythonpath 2>/dev/null Re-tying a variable fails for zsh 4.x so I have to redirect the error message to the black hole. And '-U' is for uniq to remove duplicates. -- Best regards, lilydjwg