help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] pipe character at end of command ?


From: Eric Blake
Subject: Re: [Help-bash] pipe character at end of command ?
Date: Wed, 23 Nov 2016 10:19:36 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0

On 11/23/2016 09:46 AM, Ulf Andersson A wrote:

> 
> I am at my wits end. I have searched the bash manual and man page as well as 
> numerous wikis and tutorials. 

It's there: look for the section titled Pipelines.

> 
> spunk()
> {
>    sed '/Ape/d'    |
>    sed '/Banana/d' |
>    sed '/Ladder/d'

By the way, this form is comparatively expensive (three fork/exec
pairs).  You can achieve the same results with:

spunk()
{
  sed '/Ape/d; /Banana/d; /Ladder/d'
}

which only forks once.  (In general, MOST cases of piping 'sed' or
'grep' into a second 'sed' pass are candidates for evaluation on whether
a single 'sed' invocation can do all the work itself.)

> --8><--------------------------------------------
> I have figured out what the three sed commands do each by themselves, but I 
> have still fo figure out what the pipe characters actually do here.

Without the pipelines, you are executing three separate command lines,
each with the same stdin and stdout (the first such command eats all of
stdin, so the second and third have no input to consume).  With the
pipeline, you are executing a single command line (the pipe control
operator is one of the shell operators that works without needing a line
continuation backslash); within that command line, the stdout of the
process on the left of a | is hooked to the stdin of the process on the
right.  So the sed on Ape sees your original stdin, and but only the sed
on Ladder writes to your original stdout; the other four fds (Ape stdout
and Banana stdin, and Banana stdout and Ladder stdin) are using the
results of the pipe(2) system call to pass data between processes
without it ever landing on disk.

> Clearly there is some hand waving with the pipe going on, but as I said 
> above, I have not found any kind of documentation of this behaviour.

It's documented; it's just that pipes are something that is SO
fundamental to shell operation that most users already know what they
do, so it's hard to point out good beginner's documentation to something
that so many of us already take for granted.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]