stdin stdout and stderr

In the context of computer programming languages, the standard streams are of three types i.e. ‘stdin’, ‘stdout’ and ‘stderr’. “stdin” stands for Standard Input, a stream from which a program reads its input data; “stdout” stands for Standard Output, a stream from which a program writes its output; and “stderr” stands for Standard Error, a stream typically used to output error messages.
In a command line environment, programs use these standard streams to handle input and output data.

File Descriptors with Streams

Whenever a command runs, it opens three files associated with Standard Input, Standard Output, and Standard Error. These files are associated with a unique identifier or a numerical value known as a file descriptor. In particular:

File descriptor 0Represents the Standard input stream
File descriptor 1Represents the Standard output stream
File descriptor 2Represents the Standard error (diagnostic) output stream

Operating system uses this file descriptors to manage the input and output operations. Whenever a program reads from stdin, it uses file descriptor 0. In the same way, when a program writes the output data or error message to stdout or stderr, it uses file descriptors 1 and 2, respectively.

Let’s understand all of the above streams briefly and and clear the confusion about all the three streams.

Standard Input(stdin)

Standard Input also referred to as stdin is the input stream in a computer program. It is a default source of input data for many command-line programs and scripts. It usually connects the keyboard, file, or another device.
For example When you run a command in a terminal and it prompts you for input, you can type the input directly into the terminal using standard input stream. The program reads this input from ‘stdin’.

Standard Output(stdout)

Standard Output, also referred to as stdout is the default streams where a program sends its regular output. The program can display the output on the terminal, capture it in a file, or redirect it to another program. It plays a vital role in the input and output processes of command-line programs. This stream is typically line-buffered. This means that output data is stored in a buffer and displayed when a new line is encountered or when the buffer is filled, depending on the program’s behavior.

Standard Error(stderr)

Standard Error, also referred to as ‘stderr’, is used by programs to output the results in the form of error messages, warnings, or diagnostic information. The program writes the standard error output to the screen or can redirect it to a file. It serves a specific purpose of error handling in a program. This stream is specifically designed to separate error-related output from regular program output because the output which contains errors, has a different format. Unlike stdout, stderr is unbuffered as the data written to stderr is immediately displayed in the terminal without waiting for a buffer to fill up.

How to redirect the stdout and stderr?

We can redirect both the streams of output such as Standard output(stdout) and Standard error(stderr) to a file or terminal window in in Unix-like operating systems and can react to them. By default, the Standard error output will display on the terminal. You can redirect both the output using the ‘>’ symbol. This redirection symbol works with stdout by default. You can use one of the numeric file descriptors to specify which standard output stream you want to redirect.

Use the following instruction to redirect the standard output explicitly.

1>

Use the following instruction to redirect the standard error output explicitly.

2>

Use the following instruction to redirect the output to a file.

command > output.txt

This will store the output in the file named ‘output.txt’. If the file doesn’t exist, it will be created or If it does exist, it will be overwritten.

If you want to append the output to an existing file, you can use the ‘>>’ symbol :

command >> output.txt

This will append the standard output of the command to the end of the output.txt file.

Let’s understand through the different techniques of redirection of output.

Redirecting Both stdout and stderr to a different location:

we can redirect both stdout and stderr at the same time, to two different files by using below given command, stdout and stderr will be directed to files called capture.txt and error.txt, respectively.

command 1> capture.txt 2> error.txt

To check the content of the files use the following command.

cat capture.txt
cat error.txt

Redirecting stdout and stderr to the Same Location:

Also, you can send the output of both the streams to the same file.

For this purpose, use the following command:

command > capture.txt 2>&1

We have redirected stdout and stderr to the same destination file.