$_ CLI In Depth
Learning progress 0%
lesson streams redirection pipes

Named Pipes (FIFOs)

5 min read Module 7: Streams, Redirection & Pipes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:14

An anonymous pipe with a name

The pipes you have used so far are created by the shell and disappear when the pipeline finishes. A FIFO is created with mkfifo and appears in a directory listing with the type letter p. Any process that knows its path can open it, and the kernel connects the two ends. The data moves through kernel memory, not through the disk, and no bytes are stored in the file.

Because the FIFO is a path, two processes started in different shells, or by different scripts, can use it. The processes do not need a common parent, which is what a normal pipe requires.

BASH2 lines
mkfifo chan
ls -l chan

The listing shows a line beginning with p, which marks a named pipe. The size is zero, because the file holds no data. The name chan is now a rendezvous point for any reader and writer that use it.

Blocking is the point

Opening a FIFO blocks until the other side is also open. A reader waits for a writer, and a writer waits for a reader. This is the synchronization that makes FIFOs useful: the open is the handshake. A command that writes to a FIFO with no reader will hang, and a command that reads from one with no writer will hang as well.

To avoid waiting in the foreground, start one side in the background with &. The example below starts a reader that waits, then writes a message into the pipe from the foreground.

BASH3 lines
cat chan &
echo "hello through the fifo" > chan
wait

The reader prints hello through the fifo once the writer opens the pipe. The wait builtin makes the shell wait for the background reader to finish. Without it the prompt might return before the line was printed.

Passing data between two commands

FIFOs are useful when a program needs a named input that it reads as a file, and the producer is a separate process. The pattern is to start the producer in the background writing into the FIFO, then run the consumer on the FIFO path. The consumer reads until the writer closes its end.

BASH3 lines
mkfifo feed
(printf '3\n1\n2\n' > feed &)
sort -n feed

The output is 1, 2, 3. The producer wrote three lines in the background, and sort read the FIFO as a file, sorted it, and printed the result. The writer closes when it finishes, and that end-of-file tells sort that the input is complete.

Warning

A FIFO is not storage. Data written with no reader present is not saved, and the writer blocks. Use a regular file when the data must survive, and a FIFO only when the two programs are running at the same time.

Why a FIFO needs both ends

A FIFO works only when a reader and a writer both open it. The open call on either side waits until the other side arrives, so the two programs meet at the path. This waiting is a feature for coordinating separate processes, but it is also the source of most FIFO bugs. If the writer exits without opening the FIFO, the reader waits forever. If the reader exits early, the writer receives SIGPIPE, just as it would with an anonymous pipe.

Opening a FIFO for reading and writing at once, with the bash form <>, avoids the wait on one side, but it also prevents the reader from seeing end of file when the writer closes. Use the plain read or plain write open unless you have a specific reason for the other form.

Cleaning up

A FIFO stays in the file system until you remove it, even after every process that used it has finished. Remove it with rm when you are done, and do it in scripts with a trap so the file is cleaned up on exit. A leftover FIFO is harmless to the data but confusing to the next person who finds it.

BASH2 lines
rm -f chan feed
ls -l | grep -c "^p"

The final count is 0, because both FIFOs were removed. The grep pattern matches lines that start with the type letter for a named pipe.

When to use which tool

For most work an anonymous pipe is the right choice, because it needs no file and no cleanup. Process substitution gives a file-like path without a name in the file system. A FIFO is worth the extra care when the two programs are started separately, or when a tool insists on a long-lived named input that another process feeds over time. Choosing the simplest mechanism that fits keeps scripts easier to read.

FIFOs also behave differently under interruption. If a reader is killed while the writer is still blocked, the writer may be stopped by a signal. Handling such cases is the reason real systems put a supervisor around named pipes. For a learning environment, remember the handshake and the cleanup, and you will avoid most surprises.

Terminal lab · 3 steps · real Linux in your browser

Make and use a FIFO

Create a named pipe, pass data through it, and remove it.

01
Create a named pipe
Create a FIFO named chan: ``bash mkfifo chan ``
Show hint for step 1
mkfifo takes the path of the new pipe.
02
Pass data through the pipe
Start the writer in the background, then read the pipe into msg.txt in the foreground: ``bash printf 'hello\n' > chan & cat chan > msg.txt wait ``
Show hint for step 2
Both ends must be open before either side can proceed, so one side has to run in the background.
03
Remove the pipe
Remove chan now that you are finished with it: ``bash rm chan ``
Show hint for step 3
A FIFO stays in the directory until you remove it.
Check yourself Question 1 of 4
Concept Check Creating a named pipe recall

Which command creates a named pipe in the current directory?

Recap & Key Takeaways

A FIFO is a named pipe created with mkfifo, and it appears in a listing with type p.
Opening a FIFO blocks until both a reader and a writer are present, which is how two separate processes synchronize.
A FIFO holds no data on disk, so writing with no reader waits, and data is not kept for later.
Remove FIFOs when finished, and prefer anonymous pipes or process substitution when the two programs are started together.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue