4

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

4

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.

  • 1
    In echo 'This comes on stderr' >&2, echo writes to its stdout (fd 1) which has been duplicated from its stderr. You'd need ksh/zsh print -u2 to print on fd 2. – Stéphane Chazelas Aug 25 '18 at 7:38
  • so echo command is doing stdout, ignoring shell redirections – Akhil J Apr 13 at 10:25
  • @AkhilJ echo always outputs to standard output. The shell may then redirect this. – Kusalananda Apr 13 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.