The standard streams are three data streams for input and output in the Unix operating system or related operating systems. They are also supported by the C standard library. Many programs automatically use the default input or output if no input or output files are specified on the command line.
Data streams are a continuous flow of data sets, the end of which is usually not foreseeable in advance; the records are processed continuously as soon as a new record has arrived. The individual data sets are of arbitrary but fixed type. The amount of records per time span (data rate) can vary and may become so large that the limited resources for further processing are not sufficient and the recipient has to react accordingly (e.g. discard records).
Standard input (stdin)
The standard input can be used to import data into a program. Usually, it is connected to the keyboard, which means that programs receive the user’s input through the standard input. On Unix, the default input is the device file /dev/stdin, the file descriptor is number 0.
---
For example, the default input for the less program is read from file test.txt.
1 | $ less < test.txt |
Standard output (stdout)
The standard output allows a program to output data. Usually, it is connected to the monitor, which means that programs send output texts to the user via the standard output. On Unix, the default output is the device file /dev/stdout, and the file descriptor is number 1. For example, the standard output of the find program is redirected to file example.txt.
1 | $ find . -name '*.html' > example.txt |
synonymous:
1 | $ find . -name '*.html' 1 > example.txt |
Standard error output (stderr)
The default error output is a second output stream that is designed to output error and status messages. Usually, it is also connected to the monitor; however, it can be redirected separately from the standard output so that error messages are not mixed with the output payload. On Unix, the default error output is the device file /dev/stderr, and the file descriptor is number 2.
For example, the error messages of the program find are redirected to the file newtest.txt, while the standard output is piped to the program less.
1 | $ find . -name '*.html' 2 > newtest.txt | less |

Output redirection
Example 1: Redirect the error messages in stdout:
1 | $ find . -name '*.html' 2>&1 | less |
Here it is to be understood as follows: “Redirect stderr (also) to where stdout is pointing right now.”
Example 2:
1 | $ find . -name '*.html' 2>&1 1> test.txt | less |
stderr is redirected to the screen/Scrn:, then stdout to file test.txt; stderr is not redirected to the file, because it was not redirected “into stdout”, but “to where stdout points to”. The concatenation with less causes the error messages to be displayed page by page.
Example 3:
1 | $ find . -name '*.html' 1> more_example.txt 2>&1 | less |
stdout is redirected to file more_example.txt, then stderr to where stdout is currently pointing (i.e. also to the file), there is nothing left for less!
Programming languages C and C++
In the C programming language, the stdio.h header file defines three file pointers of type FILE *, named stdin, stdout, and stderr. These virtual files are usually opened automatically from the start of the program and can be used with most commands that access files.
In C++, the IOStream Library is used, which is part of the standard library. It is integrated with the header file iostream (without file extension), the access to the standard data streams is finally via the stream objects std::cout, std::cin and std::cerr.
Since this behavior is standardized by C++ or C, these standard data streams can also be found in non-Unix operating systems (such as Windows) or are simulated accordingly.
Java programming language
In the Java programming language, three streams are created in the java.lang.System class. System.in as InputStream, System.out as PrintStream, and System.err as PrintStream. These streams are automatically opened from the start of the program and can be used to access the standard data streams. Since this behavior is standardized by Java, these standard data streams can also be found in non-Unix operating systems or are simulated accordingly.
Python programming language
The standard data streams can also be accessed under Python via the file objects stdin, stdout and stderr provided in the sys module. The provided write method can be used to write to stdout and stderr.