Linux Standard Streams – What are stdin, stdout, and stderr on Linux?

Linux is a widely used open-source operating system that powers the majority of servers, cloud platforms, and supercomputers around the world. Understanding the fundamentals of Linux is essential for anyone looking to pursue a career in IT or software development. In this “Linux Fundamental – A Journey with Linux” series, we will be exploring one of the core concepts of Linux, the Linux Standard Stream, in-depth, there are three communication channels or standard streams called stdin, stdout, and stderr.

These streams are used by a program to interact with the environment and receive input (stdin) from the user, display output (stdout) to the user, and report errors (stderr) if something goes wrong.

Think of them as pipes that connect the program to the environment and allow it to communicate.

In Linux, everything is treated as a file, including these standard streams, which get created when the program starts executing.

  1. Standard Input (stdin): This is the default input stream for a program. It is used to accept input from the user or from another program. By default, stdin is connected to the keyboard, but it can be redirected to a file or another program.
  2. Standard Output (stdout): This is the default output stream for a program. It is used to display output to the user or to another program. By default, stdout is connected to the terminal, but it can be redirected to a file or another program.
  3. Standard Error (stderr): This is the stream used for error messages. It is used to display error messages and diagnostic information to the user or to another program. By default, stderr is also connected to the terminal, but it can be redirected to a file or another program.

Overall, these three standard streams provide a standard way for programs to communicate with the operating system and with other programs in a consistent manner.

A file descriptor or file handler is a unique identifier assigned by the operating system to each open file or I/O stream. Programs use file descriptors to read from or write to files and other I/O streams. The file descriptor of stdio, stdin, and stderr are:

File Redirection

In Linux, standard streams (stdin, stdout, and stderr) are used to handle input and output from a command or program. File redirection is a technique that allows you to redirect or “send” the input or output of a command to a file instead of using the standard streams.

File redirection is an essential tool in Linux as it allows you to save and manipulate the output of commands for future use. For example, you can redirect the output of a command to a file, then read the file later or manipulate its contents with other commands.

So while file redirection and standard streams are not exactly the same topic, they are closely related and both are important concepts to understand in Linux.

Normal standard output stream which will display the result on the terminal rather not saving on the file.

The hostname the command displays the name of the current host on the standard output stream.

If the command is executed incorrectly, an error message will be displayed on the standard error stream rather than the standard output stream.

Rather than giving directly the output, we’ll store it on the host.txt by using file redirection.

It has been stored in host.txt, we check the result by cat command.

appended the new output in the existing file by using “>>“.

Standard Error

Sometimes when we run a command in Linux, we might see error messages on the screen. However, we can redirect these error messages to a file instead. For example, we can use the “LS -LAH 2> output.log" command to redirect any error messages generated by this wrong command “LS -LAH" to a file called output.log. This way, we can store the error messages in a file and view them later if we need to.

Redirecting both standard output and error to the same file:

  • ls -lah: this is the command that will be executed and whose output will be redirected.
  • >> output.log: this redirects the standard output stream (i.e., the normal output of the ls -lah command) to the end of the file output.log using the “append” mode. This means that any output from the ls -lah command will be added to the end of the file without overwriting any existing content.
  • 2>&1: this redirects the standard error stream (i.e., any error messages generated by the ls -lah command) to the same destination as the standard output stream (i.e., to the file output.log). The 2> operator redirects the standard error stream and &1 specifies that the destination of the redirection should be the same as that of the standard output stream (which is output.log in this case).

Overall, this command is useful for capturing both the normal output and any error messages generated by a command in a single file.

Getting rid of error message

-type: which type of file

-name: name of the file

The error we got was permission denied to this current user due to we don’t have privilege permission on this directory to search for the file, so we can encounter this error into /dev/null.

In Linux, “/dev/null" is a special device file that discards all data written to it and provides an endless stream of null bytes when read from. It is often used to redirect unwanted output from commands or scripts.

Printing the output and error at one time.

by using a semicolon (;) we can continue to execute the next command simultaneously in the same line, mahesh the command we tried was not available in this name on Linux. So it will give an error and we like to store this on a log.txt file.

Printing the output maheshmahesh.txt into log.txt by discarding the error message.

I hope you’ve learned and enjoyed this write-up.

So, You can connect with me on LinkedIn & Twitter for more updates on Infosec.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.