Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-14-2009
Registered User
 
Join Date: Sep 2009
Location: Kolkata,India
Posts: 265
Thanks: 17
Thanked 0 Times in 0 Posts
Difference between ">/dev/null 2>&1" and "2>&1 >/dev/null"

Does >/dev/null 2>&1 and 2>&1 >/dev/null mean the same?
Sponsored Links
    #2  
Old 12-14-2009
Scott's Avatar
Scott Scott is online now Forum Staff  
Administrator
 
Join Date: Jun 2009
Location: Zürich
Posts: 6,966
Thanks: 220
Thanked 762 Times in 666 Posts
Hi.

They're not the same.

In the first case, standard output is directed to a file (in this case /dev/null), and then standard error is directed to the same place (&1, or /dev/null).

It's equivalent to


Code:
> /dev/null 2> /dev/null

In the second case, standard error is directed to where standard output is directed at the time (i.e. the screen), and then standard output is directed to somewhere else (/dev/null)


Code:
$ cat Test
echo stdout
echo stderr >&2

$ ./Test > /dev/null 2>&1
$

$ ./Test 2>&1 > /dev/null
stderr

Sponsored Links
    #3  
Old 12-14-2009
Registered User
 
Join Date: Sep 2009
Location: Kolkata,India
Posts: 265
Thanks: 17
Thanked 0 Times in 0 Posts
Then basically ">/dev/null 2>&1" and "&> /dev/null" do the same job i guess??

Last edited by proactiveaditya; 12-14-2009 at 08:50 AM..
    #4  
Old 12-14-2009
Moderator
 
Join Date: Feb 2007
Location: The Netherlands
Posts: 7,505
Thanks: 73
Thanked 474 Times in 453 Posts
Quote:
Originally Posted by proactiveaditya View Post
Then basically ">/dev/null 2>&1" and "&> /dev/null" do the same job i guess??

Code:
&> /dev/null

Works only in Bash FWIK.
Sponsored Links
    #5  
Old 12-14-2009
Registered User
 
Join Date: Sep 2009
Location: Kolkata,India
Posts: 265
Thanks: 17
Thanked 0 Times in 0 Posts

Code:
[root@wiki ~]# echo "1" &> /dev/null
[root@wiki ~]#
[root@wiki ~]# ech "1" &> /dev/null
[root@wiki ~]#

">/dev/null 2>&1" and "&> /dev/null" does the same job
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Meaning of "> /dev/null 2>&1" salanalani Shell Programming and Scripting 10 07-22-2008 03:29 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 12:52 AM
extran NUll character added after end of line "\n" arunkumar_mca UNIX for Dummies Questions & Answers 1 11-12-2007 12:23 PM
what difference between "terminal" and "console" chenhao_no1 Programming 1 09-09-2002 09:46 AM



All times are GMT -4. The time now is 06:25 AM.