4

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.

4 Answers 4

6

You can also do this:

./program < t1.input | diff t1.expected -
5

In bash:

diff t1.expected <(./program < t1.input)
4
  • 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."
    – Ian Burris
    Jan 13, 2011 at 22:10
  • Stop putting extra spaces. Jan 13, 2011 at 22:23
  • What do you mean by extra spaces?
    – Ian Burris
    Jan 13, 2011 at 22:32
  • You have a space between the < and the (. Don't do that. Jan 13, 2011 at 22:32
4

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.

1

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

$ diff =(program1 < input1) =(program2 < input2)

You must log in to answer this question.

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