Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

4 Answers 4

up vote 3 down vote accepted

You can also do this:

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

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
add comment

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
add comment

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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