Difference between ">/dev/null 2>&1" and "2>&1 >/dev/null" | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

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

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-14-2009
proactiveaditya proactiveaditya is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 22 September 2013, 5:47 PM EDT
Location: Kolkata,India
Posts: 269
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 offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 22 November 2014, 2:17 AM EST
Location: Zürich
Posts: 8,489
Thanks: 333
Thanked 1,050 Times in 916 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
proactiveaditya proactiveaditya is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 22 September 2013, 5:47 PM EDT
Location: Kolkata,India
Posts: 269
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 09:50 AM..
    #4  
Old 12-14-2009
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 20 November 2014, 4:57 PM EST
Location: The Netherlands
Posts: 7,719
Thanks: 123
Thanked 538 Times in 508 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
proactiveaditya proactiveaditya is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 22 September 2013, 5:47 PM EDT
Location: Kolkata,India
Posts: 269
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 04:29 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
extran NUll character added after end of line "\n" arunkumar_mca UNIX for Dummies Questions & Answers 1 11-12-2007 01:23 PM
what difference between "terminal" and "console" chenhao_no1 Programming 1 09-09-2002 10:46 AM



All times are GMT -4. The time now is 04:50 AM.