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: Davide Brini
Subject: Re: [Help-bash] pipe character at end of command ?
Date: Wed, 23 Nov 2016 17:07:58 +0100

On Wed, 23 Nov 2016 15:46:17 +0000, Ulf Andersson A
<address@hidden> wrote:

> Hello,
> 
> I am at my wits end. I have searched the bash manual and man page as well
> as numerous wikis and tutorials. All to no avail. I am trying this out on
> a Red Hat Linux engine. From  uname -a I get this
> 
>        Linux themachine 2.6.32-642.6.1.el6.x86_64 #1 SMP Thu Aug 25
> 12:42:19 EDT 2016 x86_64 x86_64 x86_64 GNU/Linux
> 
> Here is my little example:
> --8><--------------------------------------------  
> #!/bin/sh
> # Ape
> # Banana
> # Ladder
> # A random comment
> 
> spunk()
> {
>    sed '/Ape/d'    |
>    sed '/Banana/d' |
>    sed '/Ladder/d'
> }
> 
> cat $0 | spunk
> --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. And
> no, I did not forget to put any continuation characters at the end of the
> lines.
> 
> The above example produces this output:
> --8><--------------------------------------------  
> #!/bin/sh
> # A random comment
> 
> spunk()
> {
> }
> 
> cat $0 | spunk
> --8><--------------------------------------------  
> 
> Now if I remove the pipe characters from the script above, I get this
> output: --8><--------------------------------------------  
> #!/bin/sh
> # Banana
> # Ladder
> # A random comment
> 
> spunk()
> {
>     sed '/Banana/d'
>     sed '/Ladder/d'
> }
> 
> cat $0 | spunk# A random comment
> --8><--------------------------------------------  
> 
> 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. At
> least not any that I could understand. I might be blind, or
> something... :)
> 
> Could anyone explain this to me, please?

In your second example, the first sed command consumes the whole input
and writes directly to stdout; the second and third sed commands are
executed but with an empty stdin, so they do nothing.

In the first example, on the other hand, the pipes allow the data to "flow"
from the first sed, to the second, to the third. 

-- 
D.



reply via email to

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