Using diff to compare the output of two commands

How does one go about using diff to compare the output of two commands?

I know how to use it to compare the contents of a file filename1 with the output of a command cmd2:

cmd | diff filename -

How do I make it so that I can have another command, say cmd1 in place of filename?

I’m using dash, which doesn’t support process substitution.

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

Based on How to emulate Process Substitution in Dash? (thanks αғsнιη!), adjusted for dash:

( cmd1 | ( cmd2 | ( diff /dev/fd/3 /dev/fd/4 ) 4<&0 ) 3<&0 )

Method 2

easy 🙂

$ cat file1 | diff /dev/stdin /dev/stderr 2<< EOT
> `cat file2`
> EOT
2c2
< 2
---
> 4


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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x