10

I am little bit confused by the following syntax. Although it works, It do not understand why it works. It seems, like there are two pipes attached to the diff command. But isn't there only one STDIN?

Examples:

diff <(echo "foobar") <(echo "barbaz")
diff <(cat foo.txt) <(cat bar.txt)
1

1 Answer 1

14

The pipes are simply bound to different file descriptors than 0 (stdin):

$ echo <(true)
/dev/fd/63
$ echo <(true) <(true)
/dev/fd/63 /dev/fd/62

A process can of course have more than one open file descriptor at a time, so there's no problem.

8
  • If I only had known its called temporary pipes, I would have been able to google it. Thank you!
    – iblue
    Commented Feb 8, 2012 at 14:24
  • @iblue: I don't think it's called "temporary pipes". It's just pipes, as created by the pipe() system call. Commented Feb 8, 2012 at 14:26
  • To be very exact, it might be called "anonymous named pipes", but it is sufficient to google it.
    – iblue
    Commented Feb 8, 2012 at 14:29
  • 2
    @WilliamPursell: There are no files involved. The shell creates an anonymous pipe using pipe() and then forks the subprocesses. The main process does have additional file descriptors open if an anonymous pipe is used. These additional file descriptors are passed in the form /dev/fd/..., and the process will usually simply open them using these file names. This will lead to them being dup()ed, creating even more open file descriptors. The process could also use the named file descriptor right away without any open calls... Commented Feb 8, 2012 at 21:46
  • 1
    ...as demonstrated in this small and stupid test program. After compiling to a, I called it as ./a <(ls), and it successfully printed the list of files, proving the named file decriptor (63 in my case) was already open. The bash might use named pipes in a temporary directory on different architectures than Linux, in which case no additional file descriptors would be open when entering the main process. Commented Feb 8, 2012 at 21:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .