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.
mkfifo chan
ls -l chanThe 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.
cat chan &
echo "hello through the fifo" > chan
waitThe 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.
mkfifo feed
(printf '3\n1\n2\n' > feed &)
sort -n feedThe 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.
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.
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.