STDIN STDOUT STDERR in Linux

This tutorial explains standard data streams in Linux. Learn what the STDIN, STDOUT, and STDERR are and how they are used in Linux through examples.

Linux is an operating system. Taking input commands, processing them and providing results are the main functions of an operating system. Typically, these tasks are performed by various components of the operating system.

To transfer data between various components, Linux uses data streams. A data stream is a flow of text data between a source, a processing unit, and a destination. A source and a destination can be any device, program or process that sends and receives data, respectively.

A processing unit sits between the source and the destination. It can be a command, script, program, process, or a processing device. It receives data from the source, processes it, and sends the processed data to the destination. If any error occurred when processing the incoming data, based on the configuration it sends the error to either the same destination or to a separate error handling destination.

Processing unit uses three different types of data streams. These streams are the input data stream, output data stream, and the error data stream. The input data stream connects the source to the processing unit. The output data stream and the error data stream connect the processing unit to the output destination and the error handling destination, respectively.

The following image shows how data flows in data streams.

examples of data stream in linux

Standards streams (STDIN, STDOUT, STDERR)

Linux defines the input data stream, output data stream, and the error data stream as the STDIN (Standard Input), STDOUT (Standard Output), and STDERR (Standard Error), respectively. Linux uses data streams as communication channels. When we open a file, execute a command, or start a process, Linux automatically attaches these channels (STDIN, STDOUT, and STDERR) to it.

Let's understand these channels in detail.

STDIN works between the source and the processing unit. The source provides the data and the processing unit receives and processes the data. Let's take a simple example to understand how STDIN works.

A user accesses a shell prompt and types a command by using the keyboard and hits the Enter key. The shell receives the command and executes it. In this process, the keyword is the source and the shell is the processing unit.

The keyboard places the typed characters (command) in the STDIN or input data stream. Since the other ends of the STDIN is connected to the shell, the shell receives the typed characters and processes them as a command.

The default device for STDIN or input data stream is the keyboard. To place data in the input data stream, you can use the default device, or can use any other source of data such as the output of any command, and a file.

The processing unit processes the command and loads the output of the command in STDOUT or output data stream. The device or application that is connected to the other end of the output data stream receives the output and based of its configuration processes the output data.

For example, if a monitor is connected to the other end of the data stream, it displays the output on the screen. Or if a printer is connected to the other end of the data stream, it prints the output.

The monitor is the default the output device. Just like STDIN, you can connect any device or application to the other end of the output data stream to receive the output or processed data.

The following image shows the flows of data between STDIN and STDOUT.

stdin and stdout example linux

STDERR or error data stream handles errors. If the processing unit finds any error when processing the received data, it loads that error in the error data stream. The device that connected to the other end of the stream handles the error.

The monitor is the default STDERR device. The monitor displays the error on the screen. The following image shows how data flows between two ends of the STDERR stream.

stdin stderr linux

Standard data streams are very flexible. You can manipulate them as you want. You can redirect the output data stream on any device, program, process, or even you can store it as a file. Same way, you can also use any file, device, or even the output of a process as the input stream.

To manipulate data streams, I/O redirections are used. To learn I/O redirections, you can check the following tutorial.

Input Output Redirection in Linux

This tutorial explains I/O redirections in detail.

That's all for this tutorial. If you like this tutorial, please consider to share it through your favorite social channel.

Advertisements

ComputerNetworkingNotes Linux Basic tutorials STDIN STDOUT STDERR in Linux