Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

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

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 12-14-2009
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 4 July 2015, 5:46 AM EDT
Location: Zürich
Posts: 8,570
Thanks: 337
Thanked 1,080 Times in 938 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 Unix and Linux 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 08:50 AM..
    #4  
Old Unix and Linux 12-14-2009
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 4 July 2015, 3:40 PM EDT
Location: The Netherlands
Posts: 7,730
Thanks: 129
Thanked 545 Times in 513 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 Unix and Linux 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 02:06 AM.