Course contentsShow
← Linux
Lesson 117 of 1,8192. Deep Dive into Bash ScriptingFree lesson

Process Substitution Techniques

Feed multiple file-like data streams into commands without temporary files.

Mastering Process Substitution

In this lesson, you will learn how to feed multiple command outputs into a single tool as if they were files, without the clutter of creating temporary storage.

The Problem: The "Temporary File" Trap

Often, you might want to compare the output of two different commands side-by-side using diff. Typically, you would run: command1 > temp1.txt command2 > temp2.txt diff temp1.txt temp2.txt

This is messy. It leaves files on your disk that you have to clean up later, and it’s slow for large data. Process substitution allows you to treat the output of a command as a file descriptor instead.

The Solution: <(command)

When you wrap a command in <(), the shell executes that command and connects its output to a special "virtual file." You can pass this virtual file path to any command that expects a filename.

For example, to compare the list of files in two different directories without creating temporary files, use:

diff <(ls /etc/default) <(ls /etc/init.d)

Here is what happens behind the scenes:

  1. The shell creates a "named pipe" or a /dev/fd/ path for the commands inside the parentheses.
  2. diff receives those paths as arguments.
  3. diff reads the output of ls as if it were reading from a standard file on your hard drive.
  4. Once the command finishes, the shell automatically cleans up the virtual file.

Why use this?

It is cleaner, faster, and keeps your filesystem pristine. You aren't just piping data into the standard input (stdin)—which only allows for one source—you are providing multiple sources that the receiving program can open, close, and read at its own pace, exactly like real files.

Key Takeaway: Process substitution using <(command) turns command outputs into virtual files, allowing you to feed multiple streams into commands like diff, cmp, or comm without creating permanent or temporary files on your disk.