Home » Headline, Linux

What does “> /dev/null 2>&1” mean?

19 June 2011 No Comment

What does /dev/null mean?If you work with Unix systems, you may come across some strange commands such as numbers with greater than sign and ampersand. To the untrained viewer, these commands may appear gibberish, but in reality they do mean something meaningful. Their compactness brings about the elegance and beauty of Unix.

When I first saw the command such as the following in a crontab file, I was confused. I did not know what to make of it. Documentation for this command was very limited and required a lot of digging around to uncover what it really meant.

/usr/bin/mycommand.sh > /dev/null 2>&1

Let me spare you the hours of frustration of trying to decipher what this command means.

Output Redirection

The greater-than sign (>) means the output of the mycommand.sh program should be redirected somewhere else. In this case, it is redirected to /dev/null. You would redirect anything to /dev/null if you do not care for the content of the output. An alternative option would be to redirect the output to a file such as the following command.

/usr/bin/mycommand.sh > /tmp/mycommand.txt 2>&1

In this example, the output of mycommand.sh program is redirected to a file called mycommand.txt in the /tmp directory.

Standard Input, Standard Output, Standard Error

In Unix, you have three standard sources of input and output for a program. Standard input is the interactive part of the program and it usually involves the keyboard input. What you enter into the program is your standard input.

After the program has done its processing, any information it wants to send back to the user is captured in the standard output. The standard output is what the program prints out to the screen to the user.

When the program enters an error of any sort, it is redirected to the standard error.

These three file descriptors are often referred to as STDIN, STDOUT, and STDERR. Other times they are not referred by names but by a built-in numbering scheme such as 0, 1, and 2, respectively. As such, STDIN can be referred as 0, STDOUT can be referred as 1, and STDERR can be referred as 2. By default, if you do not specify by name or number, you are referring to standard output.

File Descriptors

Since you are now familiar with these file descriptors, in the example above the mycommand.sh program redirects the standard output to /dev/null which means you do not really care about the output. The next command refers to standard error (2) which redirects to standard output (1). The ampersand (&) is required when you are doing redirection. Without the ampersand, what you are doing is redirecting the standard error to a file named “1”. What you are really doing is merging the data streams of standard error into standard output.

If you would rather capture the data streams of the standard error (2) and standard output (1) into a file, you would use the second example from above. In the second example, the data streams of standard output and standard error getting saved to a filed called mycommand.txt in the /tmp directory.

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.