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)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0