You might have used a syntax like
cmd0 | cmd1 | cmd2 in your terminal quite too many times.
You probably also know that it is pipe redirection which is used for redirecting output of one command as input to the next command.
But do you know what goes underneath? How pipe redirection actually works?
Not to worry because today I'll be demystifying Unix pipes so that the next time you go on a date with those fancy vertical bars, you'll know exactly what's happening.
Note: I have used the term Unix at places because the concept of pipes (like so many other things in Linux) originates from Unix.
Pipes in Linux: The general idea
Here's what you'll be seeing everywhere regarding “what are Unix pipes?”:
- Unix pipes are an IPC (Inter Process Communication) mechanism, that forwards the output of one program to the input of another program.
Now, this is the general explanation that everyone gives. I want to go in deeper. Let's rephrase the previous line in a more technical way by taking away the abstractions:
- Unix pipes are an IPC (Inter Process Communication) mechanism that takes the
stdoutof a program and forwards that to the
stdinof another program via a buffer.
Much better. Taking away the abstraction made it much cleaner, and more accurate. You can look at the following diagram to understand how pipe works:
One of the simplest example of the pipe command is to pass some command output to grep command for searching a specific string.
For example, you can search for files with name containing txt like this:
Keep in mind: Pipe redirects stdout to stdin but not as command argument
One very important thing to understand is that pipe transfers the
stdout of a command to
stdin of another but not as a command argument. I'll explain it with example.
If you use
cat command without any arguments, it'll default to reading from
stdin. Here's an example:
$ cat Subscribe to Linux Handbook for more articles like this ^D Subscribe to Linux Handbook for more articles like this
Here I used
cat without passing any files, so it defaulted to
stdin. Next I wrote a line and then used Ctrl+d to let it know that I'm done writing (Ctrl+d implies EOF or End Of File). Once I was done writing, cat read from
stdin, and wrote that very line to
Now consider the following command:
echo hey | cat
The second command is NOT equivalent to
cat hey. Here, the
stdout, "hey", is taken to a buffer, and transferred to the
cat. As there were no command line arguments, cat defaulted to
stdin for read, and there was something already in the stdin, so cat command took it, and printed to
In fact, I created a file named hey and put some content in it. You can see the difference clearly in the image below:
Types of pipes in Linux
There are two kinds of pipes in Linux:
- Unnamed pipes, also called anonymous pipes
- Named pipes
Read the full story
The rest of the article is available to LHB members only. You can sign up now for FREE to read the rest of this article along with access to all members-only posts. You also get subscribed to our fortnightly Linux newsletter.Subscribe