💾 Archived View for commacat.xyz › posts › tech › linux-streams.gmi captured on 2021-12-04 at 18:04:22. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-11-30)

-=-=-=-=-=-=-

Streams in Linux - standard input, standard output and standard error

26-03-2021

The power of Linux is in the use of three basic streams. Streams can be used so that the output of one command or program can be redirected to a file, or somewhere else. Beginners to Linux can struggle with these concepts which are essential to undestanding the power at your fingertips. There are three streams, as follows:

Standard input is stream 0, standard output is stream 1, and standard error is stream 2. These numbers can then be used to represent a stream in a command or program so that the shell, typically bash, understands what to do.

Standard output redirection

">" this means output redirection, by default you are outputting the contents of the screen/terminal, to another place:

ls directory > file.txt

This means we use the ls command to list the contents of "directory". But we do not want to see the contents of the directory on the screen, we want to redirect to a file called "file.txt". This command will create and write the output to file.txt. If file.txt exists, any content will be overwritten.

Output redirection append method

">>" This means output redirection, but when this is used you will append the output to the end of the existing file contents, and any existing file content will not be lost:

ls directory >> file.txt

Output redirection standard error

Errors may occur when you run a command. Often you want to redirect them to a file instead of printing to the screen/terminal.

"2>" This means you wish to redirect standard error only to "file.txt", all other output will be printed to the screen/terminal:

ls directory 2> file.txt

Output redirection of standard error to /dev/null

In Linux, "/dev/null" is like a virtual black hole. Anything sent here will disappear, never to be seen again. This is a useful destination for standard error that you don't want to see (like on the screen/terminal), and also don't want to keep (like in a file).

The following command will accomplish this:

ls directory 2> /dev/null

All errors from the ls command will not be displayed on the screen, but will be sent to /dev/null.

Output redirection of both standard output and standard error

"&>" This will redirect both standard output and standard error:

ls directory &> file.txt

In the preceding command, both standard output and standard error have been redirected to "file.txt". Nothing will be output to the screen/terminal.

Sometimes you want to redirect standard error to standard output

"2>&1"

ls directory 2>&1 file.txt

The input redirection operator "<"

Sometimes you want to redirect input to somewhere else

input redirection operator "<"

grep a < file.txt

The preceding command uses the contents of file.txt as input to the grep command, which will search for the letter in file.txt, and will then output the results of the command to the screen/terminal.

Summary

">" the default is always a redirection of stream 1, which means you are redirecting standard output (normally from the screen)

Both ">" and "1>" are equivalent, they mean that same thing.

"<" the default is always a redirection of stream 0, which means you are redirecting standard input (normally from the keyboard)

Both "<" and "0<" are also equivalent, they mean the same thing.