Understanding stdin, stderr and stdout in Linux

There is a decent chance that if you have used Linux operating systems then you might have encountered the three famous data streams known as stdin, stderr and stdout. All these are different in their functions and have their own uses but one thing common between all three of them is that they are data streams that bash creates.

Let’s understand more about what data streams actually mean and how they are beneficial. In terms of computing, a data stream is something that gives us the ability to transfer data from a source to an outflow and vice versa. The source and the outflow are the two end points of the data stream. It might be interesting for you to know that whatever command you are running in your Linux terminal, it will either be at one of these end points.

Now we know a little about the data streams, let’s know more about the three famous data streams.

  • stdin − It stands for standard input, and is used for taking text as an input.

  • stdout − It stands for standard output, and is used to text output of any command you type in the terminal, and then that output is stored in the stdout stream.

  • stderr − It stands for standard error. It is invoked whenever a command faces an error, then that error message gets stored in this data stream.

It should be noted that in Linux all these streams are treated as if they were files. Also, linux assigns unique values to each of these data streams.

  • 0 = stdin

  • 1 = stdout

  • 2 = stderr

Now let’s consider a few examples of these three data streams.

The example shown below depicts a typical stdin stream.




mmukul@192 Docs-Linux % read
This is to stdin

In the above command, we are providing an input to the stream and the read tool is getting the input from stdin.

Now, an example of stdout is shown below −


ls -ltr


immukul@192 Downloads % ls -ltr
total 1085456
drwxr-xr-x@ 13 immukul staff 416 Dec 7 2019 source-code-pro-release
-rw-r--r--@ 1 immukul staff 350337 Dec 22 2019 messi.jpg
-rw-r--r--@ 1 immukul staff 5953321 Dec 22 2019 927225.png
-rw-r--r--@ 1 immukul staff 601852 Dec 22 2019 238870.jpg

We know that we make use of the ls command with the -ltr flag to list all the files in a certain sorted fashion, where the lastly updated file is shown at the bottom. The list is sent to the stdout data stream and the terminal then simply prints it out.

Now, an example of stderr is shown below −


ls -ltr printit

The above command is invalid as I don’t have any directory named printit and it will generate an error message that will be sent to stderr and then the terminal will print it.


immukul@192 Downloads % ls -ltr printit
ls: printit: No such file or directory

Updated on: 31-Jul-2021

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started