Process Substitution in Bash

A nifty trick to use in bash

Fernando Villalba
factualopinions
2 min readNov 3, 2018

--

In lamest terms, process substitution allows you to turn a command into a temporary file that banishes after it finishes.

Let me give you an example:

cat <(echo hey there) # this outputs: hey there
echo <(echo hey there) # this outputs: /dev/fd/63

The first command converts the output of the command echo hey there into a file with the contents hey there

The second command echoes the name of the temporary file being used.

This may not seem very useful in this example but consider the following example where you want to compre the contents of two directories, you could do the following:

$ ls /bin/ > bindir
$ ls /usr/bin/ > usrbindir
$ diff bindir usrbindir

Or you could do this instead with process substitution:

diff <(ls /bin) <(ls /usr/bin)

Another less commonly use of process substitution takes the standard input from another command like so:

ls > >(grep file)

This will send the contents of ls to the command substitution on the right and they will be processed within the file substitution.

I couldn’t find many practical uses of this (yet) myself at work but I found this excellent blog on the same topic that offers some examples.

Additional info can be found here:

--

--