Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 the shell 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.
Understanding the Three Data Streams
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 for 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 file descriptor values to each of these data streams.
| Stream Name | File Descriptor | Purpose |
|---|---|---|
| stdin | 0 | Standard input (keyboard) |
| stdout | 1 | Standard output (display) |
| stderr | 2 | Standard error (display) |
Examples
stdin Example
The example shown below depicts a typical stdin stream.
read
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.
stdout Example
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.
stderr Example
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
Key Points
These streams allow for redirection and piping, enabling complex command combinations.
Both stdout and stderr can be displayed on the terminal simultaneously, but they remain separate streams.
Understanding these streams is essential for shell scripting and command-line operations.
Conclusion
The stdin, stdout, and stderr streams are fundamental concepts in Linux that enable data flow between commands and the terminal. Understanding these streams is crucial for effective command-line usage and forms the foundation for advanced operations like redirection and piping.
