zsh-users
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane.chazelas@gmail.com>
To: Alexander Skwar <alexanders.mailinglists+nospam@gmail.com>
Cc: zsh-users@zsh.org
Subject: Re: for loop with parameter expansion in zsh vs. bash
Date: Tue, 3 Nov 2015 08:47:57 +0000	[thread overview]
Message-ID: <20151103084757.GB6580@chaz.gmail.com> (raw)
In-Reply-To: <CADn-QaNAt3y2qd4dyBnhd9ifdMOqf9DsVwFbVfdriap6uohvRA__32237.289628438$1446536964$gmane$org@mail.gmail.com>

2015-11-03 08:47:42 +0100, Alexander Skwar:
[...]
> > IFS=:
> > for e in $~PATH

Sorry, meant $=PATH above ($= looks like scissors)

> > Or use the "s" expansion flag:
> >
> > for e in "${(s(:))PATH}"

Note that those don't work if $PATH is empty (which for command
search means one empty element) or unset (which for command
search means commands are looked in a default $PATH (whose value
depends on the shell)).

> ​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.​

Note that it is slightly more correct in zsh (that performs only
split upon command substitution) than in other shells that
perform split+glob. The use of echo causes problems with things
that start with - or contain backslashes though. The use of
command substitution means that trailing newlines in the last
element are trimmed.

set -o noglob; IFS=:; for e in $(printf %s "$PATH")

would be better (still a problem for trailing newlines).

Note that if you want to have zsh interpret sh code, you can use
"emulate -L zsh". The -L makes that "emulation" local to the
current function context.

So to share code between zsh and POSIX compliant shells, you
could do:

if [ "$ZSH_VERSION" ]; then
  alias 'START_SH_CODE=function { emulate -L sh'
  alias 'END_SH_CODE=} "$@"'
else
  START_SH_CODE() { :; }
  END_SH_CODE() { :; }
fi

And use as:

START_SH_CODE
IFS=:
set -f
for e in $PATH; do ...; done
END_SH_CODE

(note that it has the side effect of creating a function context
(the code is parsed as a whole, the positional parameters and
variables you declare inside are local to that block, "return"
breaks only out of that block.

(note that doing the same for bash (which by default, unless in
sh emulation is not POSIX compliant either though the
differences are a lot smaller) is a lot trickier as bash doesn't
have local scope for options or anonymous functions like zsh).

-- 
Stephane


  parent reply	other threads:[~2015-11-03  8: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
     [not found]   ` <CADn-QaNAt3y2qd4dyBnhd9ifdMOqf9DsVwFbVfdriap6uohvRA__32237.289628438$1446536964$gmane$org@mail.gmail.com>
2015-11-03  8:47     ` Stephane Chazelas [this message]
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=20151103084757.GB6580@chaz.gmail.com \
    --to=stephane.chazelas@gmail.com \
    --cc=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).