Pipelines#
We're going to cover the basics of pipelines or using the |
character to "pipe" the standard output (stdout
) of one command to become the standard input (stdin
) of another. Visually this looks like this:
At the top we can see a simple cat helloworld.txt
command, which prints the contents of the file (helloworld.txt
) to the terminal we're using. The stdin
to cat
is nothing - we provided nothing to its stdin
. It's stdout
was the contents of the file. Those were printed to the screen.
Below this we can see what would happen if we used | wc
at the end of the command, making it: cat helloworld.txt | wc
. The stdout
of cat helloworld.txt
is piped to the stdin
of wc
, so instead of being printed to the screen, it's "printed" (if you like) to wc
's stdin
instead. It's then processed by wc
(word count) which sends it's stdout
to the display (because there is no other pipeline), and so we see 14
in our terminal.
Let's expand with some more examples.
Check this out:
1 2 |
|
We ran out ls -l
command again, but we didn't get a list of files/directories this time. If you think about it, the output we got last time was a series of rows made up of text, wasn't it? Let's visualise that:
That output, which went to stdout
, was redirected to the wc
command using the |
(pipe). This pipeline tells Bash: take the stdout
of the command on the left, and send it to the command on the right as its stdin
. Because of this, and because the wc
command - which stands for word count
- doesn't print the file list, but instead counts words and lines, we didn't see a file listing, but we saw the number 4
.
The wc
command will count the words it finds in the stdin
. I used the -l
flag, which means, "count the lines, instead" and as we can see from the above results, there are 4
lines.
So the pipeline (|
) let us pass the output of one command and make it the input of another. This is extremely powerful, and it's what makes the command-line interface (CLI - what you're using now) very powerful. Here's a more powerful example that we're going to use later on:
1 2 |
|
Here I'm using the free
command to see how much free RAM my local system has. I'm filtering the results down only to those with Mem
in the line. I'm then only focusing on a single "column", the 9
th column, using the cut
command. You can use the man grep
and man cut
commands to read about these two new commands. They're both highly useful.