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

I have a program that writes to stdout. Is there a way that I can redirect the output to the Linux diff command or do I have to write the output to a file and then compare that?

For example, I have a bunch of test input files for a program and the corresponding expected output in another set of files. And I'd like to do something like ./program < t1.input | diff t1.expected.

share|improve this question
up vote 3 down vote accepted

You can also do this:

./program < t1.input | diff t1.expected -
share|improve this answer

In bash:

diff t1.expected <(./program < t1.input)
share|improve this answer
Do you know of anyway to do that straight from a linux command line? When I try what you posted I get "Missing name for redirection." – blcArmadillo Jan 13 '11 at 22:10
Stop putting extra spaces. – Ignacio Vazquez-Abrams Jan 13 '11 at 22:23
What do you mean by extra spaces? – blcArmadillo Jan 13 '11 at 22:32
You have a space between the < and the (. Don't do that. – Ignacio Vazquez-Abrams Jan 13 '11 at 22:32

Or the simple way using bash:

diff <(./program) <(cat t1.expected)

I often use the more general case

diff <(command1) <(command2)

where the two commands might be similar except for e.g. different input files, or different versions of a program operating on the same file. Also works with gvimdiff.

share|improve this answer

In case you want to diff two outputs of programs, zsh is your friend:

$ diff =(program1 < input1) =(program2 < input2)
share|improve this answer

You must log in to answer this question.

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