9

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)

migrated from stackoverflow.com Feb 14 '12 at 15:56

This question came from our site for professional and enthusiast programmers.

13

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.

  • If I only had known its called temporary pipes, I would have been able to google it. Thank you! – iblue Feb 8 '12 at 14:24
  • @iblue: I don't think it's called "temporary pipes". It's just pipes, as created by the pipe() system call. – Sven Marnach Feb 8 '12 at 14:26
  • To be very exact, it might be called "anonymous named pipes", but it is sufficient to google it. – iblue Feb 8 '12 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... – Sven Marnach Feb 8 '12 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. – Sven Marnach Feb 8 '12 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.