-1
  $ tty
  /dev/tty0

On

      $cat 

in user-space, cat process waits on it's stdin to receive input from /dev/tty0 file.

On listing non-existent file fff,

  ls /fff > file1
   ls cannot find file :file1 Access denied

Despite redirection of stdout to file1 instead of /dev/tty0, the error about, file does not exist, will display on /dev/tty0 through stderr file descriptor.


Question:

If stderr writes to /dev/tty0 similar to stdout, what is the purpose of stderr?

What was the idea behind, UNIX/Linux maintaining one more file descriptor stderr for writing error to /dev/tty0 ?

  • You asked the stdout to be redirected to a file AND not the stderr. Meaning, stderr is still going to the terminal, /dev/tty0. So what's the issue here? – user218374 May 7 '17 at 4:38
  • @RakeshSharma I did not get you. My question is not about knowing the mechanics in usage of stdout/stderr, but try knowing why do we need stderr, because it is also writing to /dev/tty0 – overexchange May 7 '17 at 4:41
  • Demonstrably it isn't in your example... – Michael Homer May 7 '17 at 4:49
  • @MichaelHomer ok updated query. Bit lazy today – overexchange May 7 '17 at 4:58
1

We need STDERR (file descriptor 2) to separate the output stream (STDOUT, file descriptor 1) from the error stream.

Without the separation, you can not distinguish the valid outputs from errors.

As you can see in your case, both streams are being attached to the terminal, and you can easily distinguish or operate on them (dup(2) et al.) by simple file descriptor operations from userspace.

Just an an example, to send STDOUT to a file and STDERR to a different file:

my_command >stdout.log 2>stderr.log
-3

stdoout is expected to be opened in a write-only state for every unix process from launch, stdin as read-only (as i think), and stderr as non-blocking read/write deploying - at maximum - a line out-buffer.

stderr is primarily for getting output to a consumer immediately, and is heavily relied upon in terminal user interface applications; have a look at more.

  • 1
    Both Read/write from stderr? – overexchange May 7 '17 at 6:07
  • According to the manpage on Arch Linux, stderr is unbuffered, while stdout is line-buffered. Further, I am almost completely certain that there is no system where by default stderr is RW but stdout is write-only. I'm open to learning otherwise though — any chance you could cite some sources on this? – Fox May 7 '17 at 17:04

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.