Many of you may already be familiar with the concepts of; standard input, output and error but some of you may never have heard of these concepts despite interacting with them almost every time you use a shell to launch a command or process. Essentially each command (and thus process) has three standard streams assigned to it when it is launched; one input (stdin) and two output (stdout/stderr). These streams use data in plain text forms and are usually readable by people.

Standard Input (stdin)

standard input is usually the user input from the keyboard, for example when you type information into the terminal. However, it can also be redirected output from another process. Ie you can redirect the standard output of a command, say ls, into the standard input of a file or another process. Here the standard output of ls is fed directly into the standard input of grep (using a pipe) in order to search the contents. This is an example of redirecting the standard output from one process into the standard input of another and demonstrates that output from a command can be accepted as standard input, as well as user keystrokes.

stdin

Standard Output (stdout)

command line programs send most of their output to the user via standard output. This is usually pushed out to the shell/console and is the final output of the command we have run (assuming it doesn’t return an error – in that case it should be output via stderr). An example of standard output can be seen when we run the command ls in order to view the contents of a folder or directory. The listing data is pushed out to the user via the terminal emulator or console. This output can also be sent somewhere other than the console, ie to a text file or function: this is an example of redirecting the standard output from one application to the standard input of another. In the below example, the output of ls -l is fed directly into a text file (named lsfile.txt), we can see after running cat that the contents which would usually be printed to the console have instead been written to the file.

stdout

Standard Error (stderr)

when an application generates an error (for whatever reason) the output is usually sent directly to the console, as with stdout. Although it may seem like stdout and sterr are acting in the same way because of this, the difference can be illustrated by choosing to redirect only one but no the other. In fact, they are two completely different streams: they just happen to output to the same place by default (on command line tools anyway). In the below example i have run ls -l and ls orange and redirected the standard output to a text file (>) (stdout.txt) i have also redirected the standard error to a text file (2>)(stderr). The first part of the command returns output of the listing, and is saved in the text file stdout. The second part generates an error, as no directory orange exists in this parent directory and thus it cannot list anything: this error is separated from standard output and redirected into a separate text file (stderr). I use cat to show the contents of each file afterwards.

stderr

Common Linux Redirect Operators

> redirects standard output to a file, if it already exists it’s overwritten.

>> appends standard output to an existing file, if it doesn’t exist it’s created.

| redirects standard output from one command or process into another

2> redirects standard error to a file, if it exists it’s overwritten.

2» appends standard error to an existing file, if it doesn’t exist it is created.

&> create new file, containing standard output and error.

< use the content of the specified file as standard input

> file 2 >&1 redirect stdout to a file and redirect stderr to stdout

A basic understanding of these concepts are really useful in day to day tasks within linux operating systems, as well as programs. Once you understand how processes accept input and output it’s easy to combine them to complete tasks more efficiently. You can also feed stdout from one command into another using pipes or xargs to simplify processes and build chains of logic.