[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] pipe character at end of command ?
From: |
Ulf Andersson A |
Subject: |
Re: [Help-bash] pipe character at end of command ? |
Date: |
Wed, 23 Nov 2016 17:29:34 +0000 |
My replies interspliced below.
> -----Original Message-----
> From: Greg Wooledge [mailto:address@hidden
> Sent: den 23 november 2016 17:12
> To: Ulf Andersson A <address@hidden>
> Cc: address@hidden
> Subject: Re: [Help-bash] pipe character at end of command ?
>
> On Wed, Nov 23, 2016 at 03:46:17PM +0000, Ulf Andersson A wrote:
> > 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 function is exactly equivalent to this:
>
> spunk() {
> sed '/Ape/d' | sed '/Banana/d' | sed '/Ladder/d'
> }
>
> When you write a line in such a way that there *must be more* of it in order
> to make a complete command, bash continues reading the next line to get
> the rest of the command. In these cases, an explicit \ is not needed.
Yes! That's it. Thank you.
OK, so now I know what happened. Where do I find this documented?
I am pretty sure I will need to explain this to other people in the future.
> Try it interactively, with simple cases, and you'll see:
>
> imadev:~$ echo true |
> > cat
> true
>
> imadev:~$ true &&
> > echo yes
> yes
>
> imadev:~$ echo "hello
> > world"
> hello
> world
>
> "> " is bash's default value for PS2, the internal variable that's used
> to prompt for the continuation of a multi-line command.
>
> When writing long commands in a script, most people will try to divide
> up the command in a way that makes it easy to read (for humans). When
> there are natural divisions like |, it makes sense to use these.
That is true. The old geezers way was to add the continuation characters.
Clearly things have changed since last I studied shell scripting.
> The following two multi-line commands are completely equivalent, but
> one of them is much easier to read than the other:
>
> sed '/Ape/d' |
> sed '/Banana/d' |
> sed '/Ladder/d'
>
> sed '/Ape/d' | sed \
> '/Banana/d' | sed '/Ladder/d'