0

I want to compare the output of 2 commands.

For example:

$ command1 arguments1 > tempfile1
$ command2 arguments2 > tempfile2
$ diff tempfile1 tempfile2 > savefile
$ rm tempfile1 tempfile2

Is there a clever way to do this? Hopefully in one line, and without the need for temporary files.

So, it would be something like (but obviously not exactly this):

$ diff $(command1 arguments1) $(command2 arguments2) > savefile

My machine info is:

$ uname -a
Linux host.name 3.2.54 #9 SMP Thu Feb 13 08:17:11 CST 2014 x86_64 GNU/Linux

$ which sh
/bin/sh
0

In bash, you can use process substitution:

diff <(command1 arguments1) <(command2 arguments2) > savefile

Note that this will not work in plain sh (or even bash in "posix mode", i.e. when invoked under the name "sh" and it turns off some features for compatibility). It is available in zsh, though.

  • What do you mean bash in plain-sh emulation mode? – Kevin Fegan May 26 '14 at 0:35
  • @KevinFegan: On some systems, /bin/sh is actually bash (either a link or a copy of /bin/bash); if run with this name, bash enters "posix mode" (what I called plain-sh emulation mode), and turns off a number of its advanced (and nonstandard) features. This mode can also be invoked by running bash with the --posix option, or with the command set -o posix. I'll edit my answer to clarify... – Gordon Davisson May 26 '14 at 4:01

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.