48

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)
KALAI SELVAN
  • 811
  • 2
  • 10
  • 11

2 Answers2

77

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.

John1024
  • 73,527
  • 11
  • 167
  • 163
9

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)

johan
  • 117
  • 4
Ashish Sharma
  • 91
  • 1
  • 2