Meaning of "> /dev/null 2>&1"

Hi,

I am new into UNIX shell scripting and I am wondering what is the meaning of the below text which appears at the end of each line in the ".sh" file:

> /dev/null 2>&1

For example, the line below:

sh $HOME/stats/Rep777/Act_777.sh omc omc > /dev/null 2>&1

I know for sure what "sh $HOME/stats/Rep777/Act_777.sh omc omc" does mean but I don't have any idea about the rest which is " > /dev/null 2>&1".

Thanks in advance.

it mean redirecting both standard output and error to /dev/null

So is it somehow affecting the execution of the line or it is simply does not do anything? I mean do I need to put it at the end of each line or not?

For your information, the lines I have in the ".sh" are to call another shell file which is in hand executing an SQL scripts to generate database reports.

Thanks for your fast reply.

it depends upon your requirement. if you want both error message and script output to be in same file then you can use it.

As you told that your script will be generating the Sql script then don't use the it. if you this then your sql script will have error message also, if any.

------------------
Ajay

OK, got it.

Thanks for your support.

> /dev/null 2>&1

You need to understand the theory first and then its upto you how and where you want to apply that theory. I'll try to explain above to you.

The greater-than (>) 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.

That means file descriptor 0 or fd0 denotes STDIN or standard input and file descriptor 1 or fd1 denotes STDOUT or standard output and file descriptor 2 or fd2 denotes STDERR or standard error.

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!

Regards,
Tayyab

2 Likes

Thanks so much Tayyab/shereenmotor, that's really explain the theory to me.

Hi Tayyab

Thanks for your explanation it was excellent

Ragards
Madan

Is there a way to echo something to a file and the screen at the same time? I've been echoing twice to accomplish this and would like to quit repeating myself :wink:

echo Something happened
echo Something happened >> somefile.log

Thanks,

tee is what your after.

$ echo "blah" | tee file.log
blah
$ cat file.log 
blah
$

*note: this probably should have been a new thread

Thanks! No actually, I think I should have done better searching. I got lazy.