This the multi-page printable view of this section.Click here to print.

Return to the regular view of this page.

Pipes

Pipes and Pipelines

Table of Contents

In most script languages, a pipe is where the output of one command is passed as the input of a second command. It allows the chaining of multiple commands without the need of intermediary files.

File handles

Every command has 3 files open by default:

File # Name Script Description
Read Create Append
0stdin < Input into the command
1stdout >&1 >>&1 Output from the command
2stderr >&2 >>&2 Errors from the command

1 - Here document

Sending multiple lines into a pipe

A Here document is a set of lines in a script which is to be sent to a command as it's input.

It consists of <<deliminator followed by multiple lines and then a line with just demiminator to show the end of the document.

For example, we have a 4 line document that will be sent to the command as it's standard input:

1command <<TERMINATOR
2line 1
3line 2
4line $someVar
5line 4
6TERMINATOR

See also: Here strings

2 - Here strings

Sending a string into a pipe

Here strings are similar to Here documents but allows you to do this on one line:

1command <<<"some string"

For example, the following commands do the same thing, count the number of words in a string:

1echo "This is a test" | wc -w
2
3wc -w <<<"This is a test"

You can also pipe the output of commands as a here string:

1ps -fe | wc -l
2
3wc -l <<<$(ps -fe)

See also: Here document