Compare command outputs with process substitution

Every sysadmin knows the situation, if he has to compare the output of some commands. Most of us save the output of the commands in a temporary file, compares these files with the diff binary and afterwards delete the temporary files. In this blog post I’ll show you the usage of process substitution to get the differences of commands without creating temporary files.

Situation

We’ve got two files with a list of users. Now we would like to search for usernames like user,  sort the output, get the unique entries and compare the outputs without creating a temporary file.

Solution with process substitution

There’s a nice feature called process substitution, which can be used to compare the output of programs directly.

In combination with the diff binary.

And here’s the final solution with the combination of the  diff binary and process substitution.

Conclusion

Process substitution is also a useful feature for other use cases like avoiding subshells. And it isn’t only a bash feature, it can be used with other shells like zsh or ksh as well. Just check out this page, if you like to know more about process substitution.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.