Understanding redirection in Linux

Home > Search

Redirecting only Standard Output (STDOUT)

Redirection is used to pass something to something else. To use an analogy, in sports, player1 can pass the ball to player2. In Linux, this analogy would be represented in this way.

~]# player1 > player2


As a more practical example, the phrase Hello World can be redirected to a file named example.txt.

~]# echo "Hello World" > example.txt


example.txt now contains Hello World.

~]# cat example.txt
Hello World


Technically speaking, echo "Hello World" is Standard Output (STDOUT), since the phrase Hello World is outputted to the Terminal. The > character redirects Standard Output (STDOUT) to a file. The number 1 is used to represent Standard Output. The number 1 can be placed before the > character, or just the > character can be used. Both commands are exactly the same.

~]# echo "Hello World" 1> example.txt
~]# echo "Hello World" > example.txt


A single > character will overwrite the content of the target file, and double >> will append the Standard Input to the target file. For example, the following commands adds How are you today? to example.txt.

~]# echo "How are you today?" >> example.txt


example.txt now contains Hello World and How are you today?

~]# cat example.txt
Hello World
How are you today?


As a practical example, when the /sys/fs/selinux/enforce file contains the number 1, SELinux will be set to enforcing. When the file contains the number 0, SELinux will be set to permissive. Redirecting Standard Out can be used to update the file to conain 0 or 1.

~]# echo 0 > /sys/fs/selinux/enforce
~]# echo 1 > /sys/fs/selinux/enforce


Redirecting only Standard Error (STDERR)

The bogus command will produce a Standard Error (STDERR).

~]# bogus-command
-bash: bogus-command: command not found


The number 2 is used to represent Standard Error. The number 2 must be placed before the > character to redirect a Standard Error to a file. In this example, the Standard Output of the bogus-command is redirected to example.txt. The Standard Error will not be displayed in the shell because the Standard Error was redirected to example.txt.

~]# bogus-command 2> example.txt


example.txt now contains the Standard Error.

~]# cat example.txt
-bash: bogus-command: command not found


Redirecting both Standard Error (STDERR) to Standard Output (STDOUT)

2>&1 or 1>&2 can be used to redirect both Standard Error to Standard Output to a file. For example, let's say the ls (list) command produces both Standard Output and Standard Error.

~]# ls example.txt bogus.txt
ls: cannot access bogus.txt: No such file or directory


Without 2>&1 or 1>&2, the Standard Output would be redirected to example.txt, and the Standard Error would appear in the Terminal.

~]# ls example.txt bogus.txt > example.txt
ls: cannot access bogus.file: No such file or directory


Appending 2>&1 or 1>&2 will redirect both the Standard Output and Standard Error to example.file.

  • 2>&1 redirects the Standand Error (2) to Standard Output (1), so that Standard Error becomes Standard Output. Then, all of the Standard Output is redirected to example.file.
  • 1>&2 redirects the Standard Output (1) to Standard Error (2), sot that the Standard Output becomes Standard Error. Then, all of the Standard Error is redirected to example.file.
~]# ls example.txt bogus.txt > example.txt 2>&1


Redirecting Standard Input

The < character represents Standard Input (STDIN). The number 0 is used to represent Standard Input. The < character can be used to redirect Standard Input to the Terminal. In this example, the contents of example.file will be displayed in the Terminal.

~]# < example.txt
Hello World


Redirect to /dev/null

Redirecting to /dev/null is typically used to not print output to the console. For example, perhaps you have a script that runs a number of commands, and you don't want to print the output of each and every command to the console.

The 1> /dev/null command can be used to redirect stdout to /dev/null, and 2> /dev/null can be used to redirect stderr to /dev/null. Or > /dev/null 2>&1 can be used to redirect both stdout and stderr to /dev/null.

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter in the box below so that we can be sure you are a human.