Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

From this commandlinefu post:

$ diff <(wget -q -O - URL1) <(wget -q -O - URL2)

Seems like multiple standard inputs but---if I remember my Linux Porgramming course correctly---this can't be it. I thought that, by definition, standard input was one stream.

Perhaps this has zilch to do with stdin? Can someone explain this to me; maybe provide links to documentation.

Bonus: How would a script handle these multiple streams? If someone could provide a Python or Perl example, that would be very helpful.

share|improve this question
Good question. In case you are unaware, there is a unix/linux Q&A site: – Matt Ellen Oct 20 '10 at 23:02
Documentation for process substitutions: – glenn jackman May 8 '14 at 15:03
up vote 17 down vote accepted

This isn't multiple standard input. This is a bash'ism that called 'Process Substitution'

It creates a pseudo file (/dev/fd/something) for each substitution. It's pretty useful. The command can only read as a stream, meaning it can not go back and forth with fseek. It needs to read it as a stream of bytes, like a pipe.

BONUS Answer

You don't need to do too much to use this. As far as your script is concerned, it gets a valid filename on the command line, that can be open()ed like anything else. As others have said, you'd see diff /dev/fd/XX /dev/fd/YY. If you do a stat() on any of these pseudo-files, you'll see it's a named pipe, and you should treat it with pipe semantics - namely no fseek() or ftell(). If you do a stat() test to explicitly see if it's a file (e.g. [ -f $1 ]) this will break - this is implemented as a named pipe after all.

share|improve this answer

<(...) does process substitution in bash. The output of the process in the parens is sent to an additional file descriptor beyond the normal 3, and a filename is returned corresponding to that file descriptor. In this way the output of a command can be treated as a filename to be passed to another command.

share|improve this answer

There is one stdin and one stdout for each process. They are usually connected to the terminal, but they can be redirected separately from one another.

In the example, there are two wget processes involved, each of which gets its own stdin and stdout. Each wget process writes to -, which is its stdout. Then bash's process substitution <(...) connects the stdout of the process to a unique pseudo-file, from which diff can read. Note that the two process substitutions yield two different pseudo-files! Thus, diff sees something like:

diff /dev/fd/XX /dev/fd/YY

where the stdout of wget -q -O - URL1 is connected to /dev/fd/XX, and the stdout of wget -q -O - URL2 to /dev/fd/YY.

share|improve this answer

You must log in to answer this question.

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