pcmanbob wrote: ↑
Thu Mar 26, 2020 3:44 pm
There are two places programs send output to: Standard output (stdout) and Standard Error (stderr);
You can redirect these outputs to a different place (like a file);
File descriptors are used to identify stdout (1) and stderr (2);
command > output is just a shortcut for command 1> output;
You can use &[FILE_DESCRIPTOR] to reference a file descriptor value;
Using 2>&1 will redirect stderr to whatever value is set to stdout (and 1>&2 will do the opposite).
So in this case we are redirecting both outputs to the file /home/pi/output.txt
And note that position matters, &1
refers to the current stdout at that moment
. If you accidentally put the stderr redirection before the stdout redirection then stderr would be written to the original stdout and not the new stdout.
In the following example stderr will be redirected to whatever stdout was before
stdout was redirected to output.txt
Code: Select all
my-program 2>&1 >output.txt # likely not what you wanted