From: Pavol Juhas <juhas@pa.msu.edu>
To: zsh users <zsh-users@sunsite.dk>
Subject: Re: problem piping output of shell builtin
Date: Mon, 5 Jan 2004 20:39:39 -0500 [thread overview]
Message-ID: <20040106013939.GA11204@pa.msu.edu> (raw)
In-Reply-To: <20040105213010.GA83099@quark.localdomain>
On Mon, Jan 05, 2004 at 03:30:10PM -0600, Vincent Stemen wrote:
> On Mon, Jan 05, 2004 at 03:36:38PM -0500, Pavol Juhas wrote:
> > On Mon, Jan 05, 2004 at 07:26:15PM +0000, gj@sdf.lonestar.org wrote:
...
> > > Why can't I pipe the output of 'jobs' thusly?
> >
> > AFAIK, all the shells run one side of the pipe in a subshell. bash
> > executes subshell for the right side of the pipe, however zsh does
> > so for the left side. Therefore the `jobs' command in
> > `jobs|read line' is evaluated in the subshell of zsh, which has no
> > knowledge about processes in the parent shell - and produces no
> > output. Left side subshell is however advantageous in other
> > situations, just compare
> >
> > zsh -c 'echo 10|read a; echo .$a'
> > .10
> > bash -c 'echo 10|read a; echo .$a'
> > .
...
>
> Under bash, at least, the semi-colon is ending the pipe command and
> then executing "echo .$a" as new command in the original shell. So
> you need to group the entire right side in the above example.
> ie.
>
> $ echo 10 | (read a; echo .$a)
> .10
> $
Exactly, bash has no way to read the output of the pipe to a
variable (well, without creating temporary file).
>
> That is interesting. I did not know zsh did that by default.
> However, I am not sure you are correct about zsh forking a sub-shell
> for the left side of the pipe. If so, then local shell variables from
> the parent shell should not be accessible unless they are exported,
> but they are.
>
> $ x=foo
> $ echo $x | read a; echo .$a
> .foo
> $
Variables are exported to the subshell, but if you change them in
the left-side of the pipe, they will keep the original value in the
parent shell, e.g.
$ a=foo; { a=bar } | :
$ echo $a
foo
$ a=foo; { a=bar }
$ echo $a
bar
$ a=foo; : | { a=bar }
$ echo $a
bar
$ i=foo; for i in 1 2; do echo $i; done | cat; echo $i
1
2
foo
I think the most recent versions of zsh were expanded so they
actually can handle `jobs|read line'.
Pavol
next prev parent reply other threads:[~2004-01-06 1:40 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-01-05 19:26 gj
2004-01-05 20:36 ` Pavol Juhas
2004-01-05 21:30 ` Vincent Stemen
2004-01-06 1:24 ` Bart Schaefer
2004-01-06 1:39 ` Pavol Juhas [this message]
2004-01-05 20:44 ` Vincent Stemen
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=20040106013939.GA11204@pa.msu.edu \
--to=juhas@pa.msu.edu \
--cc=zsh-users@sunsite.dk \
/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).