Redirecting output

Dan Nelson dnelson at allantgroup.com
Tue Dec 18 10:09:47 PST 2007


In the last episode (Dec 18), White Hat said:
> I am trying to find out exactly what is the difference between:
>  
> {command} 2>&1 >> /dev/null
>  
> and
>  
> {command} dev/null 2>&1
(I assume you mean >/dev/null 2>&1 )

> I have seen both used and have not been able to decipher what the
> difference is. It would seem that the first one would be the one that
> is correct.

If you want to redirect both stderr and stdout to /dev/null, the 2nd is
correct.  Your first command does this:

  assign fd 2 to whatever fd 1 is pointing to
  assign fd 1 to /dev/null

That leaves stderr going to wherever stdout usually goes (i.e. your
tty), and stdout going to /dev/null.  That might actually be what you
want, depending on the program you're running.

Your second command does this:

  assign fd 1 to /dev/null 
  assign fd 2 to whatever fd 1 is pointing to

I ran this test script with different redirections to verify what was
going on:

 #! /bin/sh
 echo I am stdout
 echo I am stderr 1>&2

-- 
	Dan Nelson
	dnelson at allantgroup.com


More information about the freebsd-questions mailing list