I remember being confused for a very long time about the trailing garbage in commands I saw in Unix systems, especially while watching compilers do their work. Nobody I asked could tell me what the funny greater-thans, ampersands and numbers after the commands meant, and search engines never turned up anything but examples of it being used without explanation. In this article I’ll explain those weird commands.
Here’s an example command:
wibble > /dev/null 2>&1
Output redirection
The greater-thans (>) in commands like these redirect the program’s output somewhere. In this case, something is being redirected into /dev/null, and something is being redirected into &1.
Standard in, out, and error
There are three standard sources of input and output for a program. Standard input usually comes from the keyboard if it’s an interactive program, or from another program if it’s processing the other program’s output. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors (you can think of them as “data pipes”) are often called STDIN, STDOUT, and STDERR.
Sometimes they’re not named, they’re numbered! The built-in numberings for them are 0, 1, and 2, in that order. By default, if you don’t name or number one explicitly, you’re talking about STDOUT.
Given that context, you can see the command above is redirecting standard output into /dev/null, which is a place you can dump anything you don’t want (often called the bit-bucket), then redirecting standard error into standard output (you have to put an & in front of the destination when you do this).
The short explanation, therefore, is “all output from this command should be shoved into a black hole.” That’s one good way to make a program be really quiet!Technorati Tags:No Tags

All the output, that is, except for error messages, right? So, one would use this structure to only listen to standard error?
Error messages go to STDERR, which I’ve redirected to STDOUT, which is being shoved into a black hole. The program should not make a peep.
To listen only to STDERR, I can just redirect STDOUT into
/dev/null, like so:wibble > /dev/nullMy source for finding this information, since I usually forget how it works, is
man bashand search for the REDIRECTION section.Some of the man page is bash specific, but most is pretty generic.
I’ve seen “2>&1 &” at the end of many tutorials, but I never knew what they were for. Thanks for the info guys.
Ah, I understand now — I misunderstood the nature of redirection, treating it as commands instead of pipes. That clears up a lot.
So logically that means you could catch STDERR from the command line by appending “2>>/var/log/logfile” to a shell command.
So, where “2” is STDERR, “>>” is to append and “/var/log/logfile” is the where errors will be logged.
Nice, Short, Sweet. So in short:
“> /dev/null” will redirect STDOUT to a BLACK HOLE
“2 > &1” will redirect STDERR to STDOUT which in turn is redirected to a BLACK HOLE
Right? Thank you for the explanation!
Yep, that’s right!
Does it mean that
>/dev/null 2>&1is equivalent to
>/dev/null 2>/dev/null?Yes, that’s true.
Excellent! Well explained.
Excellent Tutorial. Hats off Mr.Xaprb
Hi,
This explanation helps clear a lot. Thanks and keep up the good work.
Good work friend :)
Really excellent explanation, thanks!
So >/dev/null 2 > /var/log/logfile will redirect STDOUT to bit-bucket and STDERR to the log file. Am i right?
And if >/dev/null is given,will both STDOUT & STDERR be redirected to bit-bucket?
Hari, your first sentence is right, but in the second, only STDOUT goes to the bit-bucket.
If &1,&2,&3 are used to refer to STDIN,STDOUT and STDERR respectively
Then why not “wibble > /dev/null &2>&1 “
instead of “wibble > /dev/null 2>&1″
just try: ” ;tryME >> /dev/null 2>&1 ” it outputs bash: syntax error near unexpected token `;’ :))
to answer you Aman Jain I think that:
1) “wibble > /dev/null 2>1″ stdout is redirected to /dev/null and stderr is redirected to a file named “1″ (in the current directory)
2) “wibble > /dev/null 2>&1″ stdout is redirected to /dev/null and stderr is redirected to where stdout is redirected (bit bucket)
3) “wibble > /dev/null &2>1″ stdout is redirected to /dev/null , then character “&” comes, which means that this process will run in background(parallel with the shell u are using). Then comes 2>1 which will create an empt file with the name 1.
And FINALLY!!!
4) “wibble > /dev/null &2>&1 ” stdout is redirected to /dev/null. “&” means that, that process(wibble) will run in background(with stdout redirected only).Then another process is started with 2>&1 which creates no file.
U can try it. Maybe im wrong somewhere, Hey im not Steve Bourne(author of sh) ;)
a good job
Thanks a lot….Your information really helped me a lot.
Thanks to xaprb for the nice explanation..
Hi,
This is an awsome sweet tutorial. I have the following:
find . -mtime 3 -regex ‘.*\.md5$’ | xargs sudo rm -f {} \; > /dev/null 2>&1
I am trying to find files that are modified past 3 days or files more than 3 days old, remove them and redirect them to >dev/null…
When I redirect > TO /dev/null 2>&1 the entire command didn’t work, and when I removed things started working.
My question is
1. Do we need to specify the /dev/null 2>&1?
2. Is this for what went wrong - post analysis kind of thing?
Thank you
Raj
Great little article - thankyou.
good explanation..was wonderin all these days as to y we ve de > /dev/null 2>&1 in all our cronjobs…… thx… :)
echo “good work man, thx a lot” >/dev/null 2>&1;
lol
In the following statement there is a “”. Does that combination signify someting unique or is it just a input and output redirection symbol sitting side by side?
smf: :sysinit:/lib/svc/bin/svc.startd >/dev/msglog 2/dev/msglog “), then exactly what is this statement saying?
I just submitted a question and noticed that the main item I needed clarification on was stripped out. I wanted to know how the information flows within a signle command when there are multiple redirection symbols, and in this case, a input redirection symbol placed right next to an output redirection symbol. It looks like: “smf: :sysinit:/lib/svc/bin/svc.startd >/dev/msglog 2/dev/msglog /dev”. That puts the less-than and greater-than signs adjacent. (From Solaris 10 etc/initab.)
Av been using >/dev/null 2>&1 for as long as i can remmeber with no clue but now it’s clear.Thanks for the tutorial
Its a very useful info, if somebody doubted so, no easy to understand even if it looks simple, thanks god, now it apprears first in the googe search result.\
Thank you once again,.
If anyone is wondering how to refer to the greater-than sign (or angle bracket) when used in this context - some people call it a “hoinky” Probably my all-time favorite tech jargon term
grt job
Would anyone be able to explain why it wouldn’t work if you wrote it like this?
command 2>&1 >> file
This was a very helpful tutorial. Thanks again!
Why do you need the & symbol in from of 1?
If I recall correctly, “>&” redirects handles vs. “>” redirecting output to a file. If you leave out the & it would create a file named “1″
Really well done. Thanks for being so clear and concise!
Great job, thanks a lot!
Please explain difference between 2>&1 and 2&>1
2>&1: it means 2(stderr) is redirected to &1. & tells the address (fd) of 1(stdout). so the stderr is redirected to stdout.
2&>1: it means 2 that is nothing will get executed in bg because of &. And the output will be redirected to a file named 1 not the stdout.
Excellent tutorial, I’m glad people like you exist :)
Good job! I never understood this!
i feel agitated when i have to use something without knowing what its meant for. your info was a quick insight. thanks a million
Great stuff,
I have a question. Specailly on bash, if I connect to a computer with ssh and run a process in background with ./job & and try to quit the shell hangs or actually waits for the process to finish, which is not what I want. I want to quit and let the process run.
Now I have a solution to this problem, i.e, if I put ./job 2
I was trucncated..
Now I have a solution to this problem, i.e, if I put “./job 2
truncated again because of less than symbol
Now I have a solution to this problem, i.e, if I put ./job 2 [lessthan symbol instead of greaterthan]/dev/null (note the direction is inward and not outward) the shell happily exits and leaves the process in background.
Any ideas why this happens would be educating.
Thanks in advance and the article and the discussion are helpful
Xrules, look into the nohup command. Even better, though: use screen. It is one of the top 5 things I cannot live without.
Thanks
I used redirectors all the time, but the 2>&1 always confused me, very well explained!
Here’s a shortcut for directing stdout and stderr to /dev/null:
wibble >& /dev/null
Now I have a question: What if I want to redirect stdout and stderr to a file called “2″? “wibble >& 2″ doesn’t do it, of course.
>/dev/null 2>&1
is equivalent to
>/dev/null 2>/dev/null?
Then can we say like, “&” is used to trace the direction where 1 (STDOUT) directed?
Also, Is it possible to redirect only the STDERR error message to any log file (without redirecting program output to STDOUT)
Ryan:
wibble >./2 2>&1
Dinesh - see VJ’s comment above for your first question.
your second question is also answered in the original post and comments:
ls 2>stderr.log
Thanks Jt…
thanks it helped me a lot