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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Meaning of "> /dev/null 2>&1"
# 1  
Old 12-26-2006
Question 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.
# 2  
Old 12-26-2006
it mean redirecting both standard output and error to /dev/null
# 3  
Old 12-26-2006
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.
# 4  
Old 12-26-2006
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
# 5  
Old 12-26-2006
OK, got it.

Thanks for your support.
# 6  
Old 12-26-2006
Code:
> /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
These 6 Users Gave Thanks to tayyabq8 For This Post:
# 7  
Old 12-26-2006
Thanks so much Tayyab/shereenmotor, that's really explain the theory to me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies

2. Shell Programming and Scripting

Redirecting standard out to /dev/null goes to file "/dev/null" instead

I apologize if this question has been answered else where or is too elementary. I ran across a KSH script (long unimportant story) that does this: if ; then CAS_SRC_LOG="/var/log/cas_src.log 2>&1" else CAS_SRC_LOG="/dev/null 2>&1" fithen does this: /usr/bin/echo "heartbeat:... (5 Replies)
Discussion started by: jbmorrisonjr
5 Replies

3. UNIX for Dummies Questions & Answers

What is the meaning of "-s" option in "if" statement?

Hi Guys, I'm sorry but I can't find answer for this, what is the meaning of -s option in "if" statement on unix scipting. Please see sample below: opath=/home/output for i in N1 N2 N3 N4 do echo $i if then grep $i $opath/N5_CRAI > $opath/N5_$i.crai chmod 777 $opath/N5_$i.crai ... (7 Replies)
Discussion started by: rymnd_12345
7 Replies

4. Shell Programming and Scripting

find error?? find / -name "something.txt" 2>/dev/null

why is this giving me errors? i type this in: find / -name "something.txt" 2>/dev/null i get the following error messages: find: bad option 2 find: path-list predicate-list :confused: (5 Replies)
Discussion started by: magiling
5 Replies

5. Shell Programming and Scripting

Meaning of 2>/dev/null/ <<EOF ?

I am new to unix and learning. Came across this statement cmd 2>/dev/null/ <<EOF 2>/dev/null/ denotes that std error is stored in /dev/null but that is considered as a non-existent file!! What does << EOF mean and how does it affect? I am really confused.. :wall: Can some one explain this... (2 Replies)
Discussion started by: source
2 Replies

6. UNIX for Dummies Questions & Answers

/dev/null 2>&1 Versus /dev/null 2>1

How are these two different? They both prevent output and error from being displayed. I don't see the use of the "&" echo "hello" > /dev/null 2>&1 echo "hello" > /dev/null 2>1 (3 Replies)
Discussion started by: glev2005
3 Replies

7. Shell Programming and Scripting

Meaning of "b" modifier in "sort" command

I need to sort the following file by the rhdiskpower devices in the last column: Total_MB Free_MB OS_MB Name Failgroup Library Label UDID Product Redund Path 1024 851 1024 OCRVOT1_0000 OCRVOT1_0000 System UNKNOWN ... (3 Replies)
Discussion started by: wjssj
3 Replies

8. UNIX for Dummies Questions & Answers

the meaning of "!:*" in "alias foo 'command\!:*' filename"

Hi: How can I remove my own post? Thanks. (2 Replies)
Discussion started by: phil518
2 Replies

9. Shell Programming and Scripting

Difference between ">/dev/null 2>&1" and "2>&1 >/dev/null"

Does >/dev/null 2>&1 and 2>&1 >/dev/null mean the same? (4 Replies)
Discussion started by: proactiveaditya
4 Replies

10. UNIX for Dummies Questions & Answers

Meaning of $var->{"@$row[0]"}=" "; ???

while (my $row = $sth->fetchrow_arrayref) { $var->{"@$row"}=" "; } Can anyone help me understanding above mentioned. i) As per my knowledge $row is taking ARRAY Refernce from the database ii) @$row is containing the value of 0th index of the array, testted the same. but I am not able... (0 Replies)
Discussion started by: jaigs_27
0 Replies
Login or Register to Ask a Question