[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] pipe character at end of command ?
From: |
Greg Wooledge |
Subject: |
Re: [Help-bash] pipe character at end of command ? |
Date: |
Wed, 23 Nov 2016 11:11:51 -0500 |
User-agent: |
Mutt/1.4.2.3i |
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.
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.
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'