Stdin, stdout and stderr

There are three types of standard streams for Unix-like operating systems. Standard streams are preconnected input and output channels between a computer program and its environment (typically a text terminal) when it begins execution. The three I/O connections are called standard input, standard output and standard error.

From Wikipedia:

Standard input (stdin)

Standard input is data (often text) going into a program. The program requests data transfers by use of the read operation. Not all programs require input. For example, the dir or ls program (which displays file names contained in a directory) performs its operation without any stream data input. Unless redirected, input is expected from the text terminal which started the program.

Standard output (stdout)

Standard output is the stream where a program writes its output data. The program requests data transfer with the write operation. Not all programs generate output. For example the file rename command (variously called mv, move, ren) is silent on success.

Standard error (stderr)

Standard error is another output stream typically used by programs to output error messages or diagnostics. It is a stream independent of standard output and can be redirected separately. The usual destination is the text terminal which started the program to provide the best chance of being seen even if standard output is redirected (so not readily observed). For example, output of a program in a pipeline is redirected to input of the next program, but errors from each program still go directly to the text terminal.

It is acceptable—and normal—for standard output and standard error to be directed to the same destination, such as the text terminal. Messages appear in the same order as the program writes them, unless buffering is involved. (For example, a common situation is when the standard error stream is unbuffered but the standard output stream is line-buffered; in this case, text written to standard error later may appear on the terminal earlier, if the standard output stream’s buffer is not yet full.)


Now that we’ve explained it somewhat, lets get some hands on action to understand it a bit better:

Typing commands in shell will result in two output streams namely stdout and stderr. Stdout is the normal output stream while stderr is the output stream for error. For example, if you type the below command,

  • ls /usr/ /user

ls: cannot access /user: No such file or directory
/usr/:
bin games java lib local share tmp etc include kerberos libexec sbin src

The “ls: cannot access /user: No such file or directory” line is the stderr. The stdout lines are:

“/usr/:

bin games java lib local share tmp etc include kerberos libexec sbin src ”
By default, all output will be directed to the screen, but you can redirect both stdout and stderr to a file.

To redirect stdout to a file named /home/username/stdout and display the stderr to the screen:

  • ls /usr /user > /home/username/stdout

ls: cannot access /user: No such file or directoryAs you can see the stderr ” ls: cannot access /user: No such file or directory” printed on the screen, but now we have a file “/home/username/stdout” that if you open it will contain the stdout output. You can “nano stdout” to check the contents of that file to ensure that worked correctly.

To redirect stderr to a file named /home/username/stderr and display stdout to the screen:

  • ls /usr /user 2> /home/username/stderr

/usr:
bin games java lib local share tmp etc include kerberos libexec sbin src

Now, out stderr file contains ” ls: cannot access /user: No such file or directory” while the screen printed the stdout:” /usr:bin games java lib local share tmp etc include kerberos libexec sbin src”. Note, the “>2″ specified we wanted stderr. What we name the files really doesn’t matter.

To redirect both to the file named /home/username/all:

ls /usr /user > /home/username/all 2>&1

Now you should have a file named all that contains both the stdout and stderr. Do a “nano all” to check the file.

To append the stderr and stdout to a file, simply:

ls /usr /user >> /home/username/all 2>&1

This adds the new stderr and stdout to the already existing “all” file. If you “nano all” once again, you’ll see that information has been added to the file, rather than been over written. Specifying “>” sends the information to a file. Specifying “>>” appends new information to the bottom of the file.

To both redirect and display the stderr and stdout, use tee:

ls /usr /user 2>&1 | tee /home/username/all2

ls: cannot access /user: No such file or directory
/usr:
bin etc games include java kerberos lib libexec local sbin share src tmp

  • tee reads from standard input and write to standard output and files
  • 2>&1 states you want both stdout and stderr
  • | - is a pipe it tells the first command to “pipe” the information to a second command after completeing. Without the “|” section and everything after it, the information would simply display on the screen and not go to the file all2. With the pipe it and the tee command, it does both.

If you do not want to see any error display, just redirect the stderr to /dev/null

ls /user 2> /dev/null

/dev/null or the null device is a special file that discards all data written to it (but reports that the write operation succeeded), and provides no data to any process that reads from it (it returns EOF). In Unix programmer jargon, it may also be called the bit bucket or black hole.

Related Posts

Tags: , , ,

Leave a Reply

You must be logged in to post a comment.