When merging output and errors, the shell evaluates the command from left to right, which is really all you need to know when putting mergers together. Here’s an example.
$ cleanup >cleanup.out 2>&1
In the above example the script cleanup directs all output (>) to a file called cleanup.out, and all errors (2), are directed to (>) the same place as the output (&1), which is cleanup.out.
$ grep "standard" * > grep.out 2>&1
In the above example all output from the grep command is put into the output file grep.out. When you use here documents, you will probably need to capture all the output to a file in case errors are encountered. To do this you need to use 2>&1 as part of the command. Here’s ...