Shell – How to see from which file descriptor output is coming

linuxoutputshellstderrstdout

How to see from which file descriptor output is coming?

$ echo hello  
hello  
$ echo hello 1>&2  
hello

all are going to /dev/pts/0
but there are 3 file descriptors 0,1,2

Best Answer

Ordinary output happens on file descriptor 1 (standard output). Diagnostic output, as well as user interaction (prompts etc.), happens on file descriptor 2 (standard error), and input comes into the program on file descriptor 0 (standard input).

Example of output on standard output/error:

echo 'This goes to stdout'
echo 'This goes to stderr' >&2

In both instances above, echo writes to standard output, but in the second command, the standard output of the command is redirected to standard error.

Example of filtering (removing) one or the other (or both) of the channels of output:

{
    echo 'This goes to stdout'
    echo 'This goes to stderr' >&2
} >/dev/null   # stderr will still be let through

{
    echo 'This goes to stdout'
    echo 'This goes to stderr' >&2
} 2>/dev/null   # stdout will still be let through

{
    echo 'This goes to stdout'
    echo 'This goes to stderr' >&2
} >/dev/null 2>&1   # neither stdout nor stderr will be let through

The output streams are connected to the current terminal (/dev/pts/0 in your case), unless redirected elsewhere as shown above.

Related Question