- [Instructor] A pipe is a way
of one program communicating with another.
A common use for a pipe
is taking the output of one command,
and sending it to the input of another command.
For instance, we could grep a file,
and if the output was too long to fit on the screen,
we could pipe the output to less, a pager.
Type into a terminal grep tcp /etc/services | less
and hit enter.
This looks through the etc services file for the word tcp
and then sends the output to less,
which presents it one page at a time.
Press q to quit.
You can pipe any number of times,
as it's not limited to just two commands.
For example, type in
grep tcp /etc/services | awk '{print $1}' | sort | less
This searches the etc services file for the word tcp,
and then sends the lines to awk,
which prints out the first column only,
then sends the remaining output to sort,
which sorts it, and finally sends it to less
to view one page at a time.
Hit enter.
The type of pipes we are using are considered unnamed pipes.
An unnamed pipe is a direct connection between two commands
running in the same terminal.
If we want to send output from a command in one terminal
to another command in a different terminal,
we can use a named pipe or FIFO.
FIFO stands for first in, first out.
This is a pipe that exists in the file system.
The pipe acts like a file on a disc
that one process can write to, while another reads from.
A named pipe is still a direct connection between programs.
It just uses the file system as an interface.
When one process is writing to the named pipe,
its output gets blocked
until the receiving process starts reading it.
Let's take a look at how this works.
To set this up, let's open a second terminal
by going to our file menu, and down to open terminal.
This terminal, we're going to drag to the right,
and bump the font size.
I'll drag the original terminal to the left,
so it fills up the remaining space.
Press q to get out of less.
Now we're going to create our named pipe.
In the left-hand terminal, type in mkfifo named_pipe
and hit enter.
This makes a pipe called named_pipe.
This name is arbitrary.
You can choose a different name if you wish.
Now, let's send some data to this pipe
using echo and a redirect.
Type in echo "Hi" > named_pipe and hit enter.
Notice that I'm in my home directory
in both of these terminals.
Also notice that we didn't get our terminal back.
This is because the I/O is being blocked.
The echo command can't finish printing the word Hi
until another process reads it from the named pipe.
Now, in the second terminal, let's use cat to read the pipe.
Type in cat named_pipe and hit enter.
We see that as soon as cat reads the output
from the named pipe, the echo command was able to finish.
We can use named pipes to send output
from one command in the OS,
to another command without them running in the same shell,
or even by the same user.
We don't need our second terminal anymore,
so close it and make the first one full-screen.
Now, let's take a look at the metadata
of the named pipe using ls.
Type in ls -l and hit enter.
You'll notice at the left-hand column,
we can see that the left-most character is a p for pipe.
To get rid of a named pipe,
you can delete it like any other file.
Type in rm named_pipe and hit enter.