Redirect Output
Chester Wyke November 13, 2024 Updated: April 15, 2025 #bashstderr to stdout
Use 2>&1
In the example without the redirect there is no output in vscode because tar
requires arguments and instead prints to stderr
|
If sending to a file instead of piping stdout
then redirect stdout
first (the order the redirects are done in matters).
As it does the redirects from left to right as per SA Question. (See excerpt below)
cmd >>file.txt 2>&1
Bash executes the redirects from left to right as follows:
>>file.txt
: Open file.txt in append mode and redirect stdout there.2>&1
: Redirect stderr to “where stdout is currently going”. In this case, that is a file opened in append mode. In other words, the &1 reuses the file descriptor which stdout currently uses.