GNU Info

Info Node: (zsh.info)Process Substitution

(zsh.info)Process Substitution


Next: Parameter Expansion Prev: History Expansion Up: Expansion
Enter node , (file) or (file)node

Process Substitution
====================

Each command argument of the form `<(LIST)', `>(LIST)' or `=(LIST)' is
subject to process substitution.  In the case of the < or > forms, the
shell runs process LIST asynchronously.  If the system supports the
/dev/fd mechanism, the command argument is the name of the device file
corresponding to a file descriptor; otherwise, if the system supports
named pipes (FIFOs), the command argument will be a named pipe.  If the
form with > is selected then writing on this special file will provide
input for LIST.  If < is used, then the file passed as an argument will
be connected to the output of the LIST process.  For example,

     paste <(cut -f1 FILE1) <(cut -f3 FILE2) |
     tee >(PROCESS1) >(PROCESS2) >/dev/null

cuts fields 1 and 3 from the files FILE1 and FILE2 respectively, pastes
the results together, and sends it to the processes PROCESS1 and
PROCESS2.

Both the /dev/fd and the named pipe implementation have drawbacks.  In
the former case, some programmes may automatically close the file
descriptor in question before examining the file on the command line,
particularly if this is necessary for security reasons such as when the
programme is running setuid.  In the second case, if the programme does
not actually open the file, the subshell attempting to read from or
write to the pipe will (in a typical implementation, different
operating systems may have different behaviour) block for ever and have
to be killed explicitly.  In both cases, the shell actually supplies the
information using a pipe, so that programmes that expect to lseek (see
man page lseek(2)) on the file will not work.

Also note that the previous example can be more compactly and
efficiently written (provided the MULTIOS option is set) as:

     paste <(cut -f1 FILE1) <(cut -f3 FILE2) > >(PROCESS1) > >(PROCESS2)

The shell uses pipes instead of FIFOs to implement the latter two
process substitutions in the above example.

If = is used, then the file passed as an argument will be the name of a
temporary file containing the output of the LIST process.  This may be
used instead of the < form for a program that expects to lseek (see man
page lseek(2)) on the input file.


automatically generated by info2www version 1.2.2.9