How can I use the diff command to compare 2 commands' outputs?

Does something like this exist?

diff  ($cat /etc/passwd) ($cut -f2/etc/passwd)
  • 1
    Of course, if one of the commands is really a cat with a single file argument, there's no obvious reason not to use that filename as one of the arguments to diff. – G-Man Sep 16 '14 at 16:03
  • same question on stackoverflow with 250+ votes: stackoverflow.com/questions/3800202/… – Trevor Boyd Smith Aug 1 '17 at 18:02
up vote 15 down vote accepted

Use process substitution:

diff <(cat /etc/passwd) <(cut -f2 /etc/passwd)

<(...) is called process substitution. It converts the output of a command into a file-like object that diff can read from.

While process substitution is not POSIX, it is supported by bash, ksh, and zsh.

Difference between 2 commands output :-

$ diff <(*command1*) <(*command2*)

Difference between command output and file :-

$ diff <(*command*) *filename*

Difference between 2 files :-

$ diff *file1 file2*

e.g. $ diff <(mount) <(cat /proc/mounts)

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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