Bash: How to redirect standard error to standard out
You have a program which is outputting information to standard error that you wish to search through. When commands are chained together in Unix via the pipe operator, standard out is connected to standard in. Thus you cannot easily search the contents of the standard error. How can you find what you’re looking for?
The first solution is to save the standard error as a file, and search through the file.
command_producing_standard_error 2> stderr.txt; grep "search string" stderr.txt; rm stderr.txt
This works but you have to remember to remove the text file that’s created in the process.
A better solution, and one that allows you to use the standard error in an existing pipeline is to instead redirect standard error to standard out.
command_producing_standard_error 2>&1 | grep "search string"
Recall that 2 refers to standard error and 1 refers to standard out; those familiar with C/C++ should recognize ‘&’ as the address operator, and it serves a similar role here. After this command, both the standard out and standard error are in one stream, standard out, and can be connected via the pipe (|) symbol to other programs, such as grep.
This tip is modified from information found in the Bash Cookbook, in the recipe “Saving Output When Redirect Doesn’t Seem To Work”. Additional solutions and discussion can be found on unix.stackexchange.com.