9.18 Process Substitution
Do you find yourself making temporary files, then giving those files to some commands to read? For example, maybe you want to compare two files with comm (28.12 ) - but comm needs sorted files, and these files aren't sorted. So you have to type:
There are easier ways to do that.
9.18.1 bash Process Substitution
has the operator
(In the first column, comm shows lines only in file1 . The second column shows lines only in file2 . The third column shows lines that were in both files.)
Let's take a closer look at how that works.
By setting the
will display the processes it runs with a
The script made its named pipes in /tmp . bash ran each sort command, sent its output to a named pipe, and put the pipe's filename on the command line. When the comm program finished, the named pipes were deleted.
I've run into problems with this operator in some cases:
when the process reading from a named pipe "hung" and never made any
For example, that happened when I replaced comm
I'd get no output from diff
I worked around the problem by
closing the standard output of each process with the
That made diff happy; it showed me the differences between the two sorted files.
also has a similar operator,
9.18.2 Automatic Temporary Files with !
If you don't have bash
, you can use the
shell script named
Why didn't I use the command line below, without the
That's because the comm program (like most UNIX programs) needs filename arguments. Using backquotes by themselves would place the list of names (the sorted contents of the files file1 and file2 ) on the comm command line.
To see what's happening, you can use
a Bourne shell and set its
will display the commands it runs with a
The script made its temporary files (21.3 ) in /tmp . You should probably remove them. If you're the only one using this script, you might be able to get away with a command like:
If your system has more than one user, it's safer to use find (17.1 ) :
If you use this script much, you might make that cleanup command into an alias (10.2 ) or a shell script - or start it in the background (1.26 ) from your .logout file (3.1 , 3.2 ) .
Here's the ! script. Of course, you can change the name to something besides ! if you want.