In this lesson, you will learn how to use process substitution to feed the output of a command into another tool as if it were reading from a physical file on your disk.
The Problem: Files vs. Streams
Many command-line tools in Linux are designed to accept a file path as an argument. For example, diff file1.txt file2.txt compares two saved files. But what if you want to compare the output of two commands, like listing the files in two different directories? Normally, you would have to redirect those outputs to temporary files first:
ls dir1 > temp1.txt
ls dir2 > temp2.txt
diff temp1.txt temp2.txt
rm temp1.txt temp2.txt
This is tedious, creates clutter, and leaves behind temporary junk if your script crashes.
The Solution: Process Substitution
Process substitution allows you to treat the output of a process as a file path. The syntax <(command) acts as a "virtual file." When the shell executes this, it creates a pipe and presents it as a filename (e.g., /dev/fd/63) that the command can read.
Using this, our previous example becomes a single, clean line:
diff <(ls dir1) <(ls dir2)
In this command, diff thinks it is reading two regular files. In reality, the shell is running ls dir1 and ls dir2 in the background and streaming their output directly into diff via file descriptors.
Why use this?
- Efficiency: It avoids writing to the disk, keeping data in memory and pipes.
- Cleanliness: It eliminates the need to create, manage, and delete temporary files.
- Flexibility: You can use this with any tool that expects a file path argument (like
grep,cat, orcomm).
You can also use >(command) to send output to a process as if it were a file being written to, which is useful for logging or streaming data between tools without intermediate storage.
Key Takeaway: Process substitution <(command) allows you to treat the ephemeral output of a command as a file, enabling you to use tools that require file arguments without needing to create permanent disk files.