An Introduction to Named Pipes

published August 19, 2023; re-published March 26, 2024

Have you ever wanted the output of one command to be used as input for another? There are many ways to do this in programming; however, the command line is an entirely different arena. Fortunately, there is a feature on Linux systems that makes this simple and easy to use: Pipes.

$ ls

$ grep x

Let's look at the above commands. We know that "ls" as a command lists all of the files in a directory, and "grep x" prints the contents of a subject that contain "x". In their current state, these two commands cannot work together, separated by lines on the bash terminal. In order to make them work, we need to use what's called an unnamed pipe.

$ ls | grep x

It's almost too simple. The singular line is read as a pipe, a connection between the two commands. The pipe funnels (or "pipes") the output of the first command, "ls", into the input of the second, "grep x". The two commands act as one, and the output you receive is a combination of their efforts. That is how an "unnamed pipe" works.

Now unto "named pipes". Unnamed pipes work simply, allowing communication between commands on a single terminal; a named pipe is a file, usually as FIFO file, and connects the processes of two or more applications in a similar line of communication. You create a named pipe via mkfifo, like so:

$ mkfifo mypipe

Now to use a named pipe, you can run a command through one terminal:

$ echo "Let's a-go!" > mypipe

And then open mypipe in another terminal to see the output of that command:

$ cat < mypipe
Let's a-go!

Named pipes are incredibly useful for communication between programs on Linux.


Sources:

https://www.linuxjournal.com/article/2156

https://yewtu.be/watch?v=DFreHo3UCD0

<--return to the Library
` Exodite.org | 2023-2024