Understanding redirection in Linux

Home > Search
  by

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.

[john.doe@server1 ~]# player1 > player2

 

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

[john.doe@server1 ]# echo "Hello World" > example-file

 

Example.file now contains Hello World.

[john.doe@server1 ]# cat example-file
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.

[john.doe@server1 ]# echo "Hello World" 1> example-file
[john.doe@server1 ]# echo "Hello World" > example-file

 

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-file.

[john.doe@server1 ]# echo "How are you today?" >> example-file

 

Example.file now contains Hello World and How are you today?

[john.doe@server1 ]# cat example-file
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.

[root@server1 ~]# echo 0 > /sys/fs/selinux/enforce
[root@server1 ~]# echo 1 > /sys/fs/selinux/enforce

 


Redirecting only Standard Error (STDERR)

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

[john.doe@server1 ]# 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.file. The Standard Error will not be displayed in the shell because the Standard Error was redirected to example.file.

[user1@server1 ]# bogus-command 2> example.file

 

Example.file now contains the Standard Error.

[john.doe@server1 ]# cat example-file
-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.

[john.doe@server1 ]# ls real.file bogus.file
ls: cannot access bogus.file: No such file or directory
real.file

 

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

[john.doe@server1 ]# ls real.file bogus.file > example.file
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.
[john.doe@server1 ]# ls real.file bogus.file > example.file 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.

[user1@server1 ]# < example.file
Hello World

 



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.




Comments