What is the portable (POSIX) way to achieve process substitution?
That feature was introduced by ksh
(first documented in ksh86) and was making use of the /dev/fd/n
feature
(added independently in some BSDs and AT&T systems earlier).
In ksh
and up to ksh93u, it wouldn't work unless your system had support for /dev/fd/n
. zsh, bash and ksh93u+
and above can make use of temporary named pipes (named pipes added in System III)
where /dev/fd/n
are not available.
On systems where /dev/fd/n
is available (POSIX doesn't specify those),
you can do process substitution (e.g., diff <(cmd1) <(cmd2)
) yourself with:
{
cmd1 4<&- | {
# in here fd 3 points to the reading end of the pipe
# from cmd1, while fd 0 has been restored from the original
# stdin (saved on fd 4, now closed as no longer needed)
cmd2 3<&- | diff /dev/fd/3 -
} 3<&0 <&4 4<&- # restore the original stdin for cmd2
} 4<&0 # save a copy of stdin for cmd2
However that doesn't work with ksh93
on Linux as there, shell pipes are implemented with socketpairs instead of pipes and opening /dev/fd/3
where fd 3 points to a socket doesn't work on Linux.
Though POSIX doesn't specify /dev/fd/n
, it does specify named pipes. Named pipes work like normal pipes except that you can access them from the file system. The issue here is that you have to create temporary ones and clean up afterwards, which is hard to do reliably, especially considering that POSIX has no standard mechanism (like a mktemp -d
as found on some systems) to create temporary files or directories, and signal handling (to clean-up upon hang-up or kill) is also hard to do portably.
You could do something like:
tmpfifo() (
n=0
until
fifo=$1.$$.$n
mkfifo -m 600 -- "$fifo" 2> /dev/null
do
n=$((n + 1))
# give up after 20 attempts as it could be a permanent condition
# that prevents us from creating fifos. You'd need to raise that
# limit if you intend to create (and use at the same time)
# more than 20 fifos in your script
[ "$n" -lt 20 ] || exit 1
done
printf '%s\n' "$fifo"
)
cleanup() { rm -f -- "$fifo"; }
fifo=$(tmpfifo /tmp/fifo) || exit
cmd2 > "$fifo" & cmd1 | diff - "$fifo"
cleanup
(not taking care of signal handling here).