Understanding Stdin, Stdout, and Stderr Streams in Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

If you ever used vim, nano, or read commands, you were already using the stdin stream.

Or if you were listing the files and directories from your system using the ls command, you were using the stdout stream.

In case you misspelled your command or interpreted something wrong, you were using the stderr stream.

I mean to say that if you were interacting with a Linux terminal and performing any command execution or piping the two commands, you already had them in use.

Tutorial Details

DescriptionStdin, Stdout, and Stderr Streams
Difficulty LevelModerate
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitescat, echo, read, ls
Internet RequiredNo
  • stdin a.k.a. β€œstandard input,” will take the text as input.
  • stdout a.k.a. β€œstandard output”, is the output of a command that is stored in this stream.
  • stderr a.k.a. β€œstandard error”, stores the command error or diagnostic information.

Also, Linux assigns unique values to each of these data streams.


In Linux, you do not need to find this file or perform any action for them; they are already part of your Linux system and attached to your terminal device (e.g., β€œ/dev/ttyβ€œ).

Whenever you execute any command in your terminal, this stream will sit in the back and watch the command you execute, and then the one that is required will come into use.

For example, if you execute the read command in your terminal, it will ask for input from your keyboard taking the use of stdin stream.

$ read
Hello folks, this is the stdin example from linuxtldr.com


usage of stdin stream in read command

Now, if you were listing the current directory, you would execute the β€œls” command, which would return or stream the data to stdout, and the terminal would print it out.

$ ls -l


usage of stdout stream in ls command

However, if I misspell the same command above as shown,

$ ls -l randomstring


demonstrating stderr stream using ls command

Above, I provided an invalid argument to the β€œls” command, which resulted in the β€œNo such file or directory” error that is returned to stderr stream by the β€œls” command.

Piping the Two Commands

Using the stdin, stdout, and, in some cases, stderr can be used to pipe one command output as another command input, and vice versa, using the β€œ|” sign.

For example, you can use the β€œ|” sign to pipe the echo command output (stdout stream) to the grep command (stdin stream) as shown.

$ echo "Hello folks!" | grep Hello


stdout and stdin stream example

If you want to pipe stdout and stderr to the next command, use the β€œ|&” instead.

Without error:

$ echo "Hello folks!" |& cat


piping stdout and stderr to next command without error

With error:

$ randomstring |& cat


piping stdout and stderr to next command with error

Redirecting the Commands

In the above, you learn to pipe two different commands using the β€œ|” sign and redirect stdout or stderr streams’ data to the stdin stream.

However, you can use an β€œ>” sign to write this data into a file or redirect it to the another command as argument.

$ echo "Hello folks!" > file.txt


stdout and stdin stream example

The above command will take the stdout stream data from the echo command and redirect it to the target file (ex: β€œfile.txtβ€œ).

Remember, when you execute the above command in the present directory, if there is no β€œfile.txtβ€œ, it will create a fresh file.

But if there is already a file with the same name as the target file in the present directory (ex: β€œfile.txtβ€œ), it will overwrite it by removing the present content with new content (β€œHello, folks!β€œ) into the file.

However, you can prevent this from happening by using the β€œ>>” double redirection symbol, which will write the stdout stream data at the end of the target file.

$ echo "Hello folks!" >> file.txt


Appending new content to a file without removing the older content

However, you can also take the stdin stream data as an input from another command instead of using the keyboard, as shown.

$ cat < file.txt 


using stdin stream from another command

The above cat command is working as stdin data (without the use of a keyboard) from the file (ex, β€œfile.txtβ€œ), which is working as stdout data.

Redirecting the stdout and stderr

Until now, we were redirecting the stdout data to stdin and vice versa. However, you can also redirect the stdout and stderr streams together to the stdin stream using their unique values, as shown.

$ echo "Hello folks!" 1> output.txt 2>errors.txt


redirecting stdin and stderr stream output to a new file

The above command will not throw an error, so only stdout data will be redirected to stdin, and β€œoutput.txt” will have the β€œHello folks!” content.

However, if you execute the wrong command or misspell it, as shown.

$ randomcommand 1> output.txt 2>errors.txt

It will only modify the β€œerrors.txt” file with the error message that came from the stderr stream, as shown.

redirecting stdin and stderr stream output to a new file with wrong command

That’s all for now. I tried to explain this concept to you as simply as possible for beginners to understand.

They are simply used to stream data from one point to another, much like rivers flow from one end to another.

In case you are unable to understand anything or are still unclear with the concept, do let us know in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.