[vox-tech] Re: quick, stupid bash question

vox-tech@lists.lugod.org vox-tech@lists.lugod.org
Wed, 29 May 2002 22:19:37 -0400


On Wed, May 29, 2002 at 05:10:14PM -0700, Ken Bloom wrote:
> ./ascript 1>&2 2>&1 #sends everything to standard error
> ./ascript 2>&1 1>&2 #sends everything to standard output
> 
> As powerful as this is, there's no way to swap standard error and
> standard output.

  There are several ways to swap stdout and standard error... you just need
a third fd to complete the swap operation.

  ( ./ascript 3>&2 2>&1 1>&3 ) # HERE

  ( exec 3>&2 2>&1 1>&3; ./a; ./bunch; ./of | ./things ) # HERE


  Where "HERE" is above things are swapped.  Sample follows...


msimons@star:~/debian$ cat ascript
#! /bin/bash
echo "something on stderr" 1>&2
echo "something on stdout"
msimons@star:~$ ./ascript
something on stderr
something on stdout
msimons@star:~$ ./ascript > /dev/null
something on stderr
msimons@star:~$ ./ascript 2> /dev/null
something on stdout
msimons@star:~/debian$ ./ascript > /dev/null
something on stderr
msimons@star:~/debian$ ./ascript 2> /dev/null
something on stdout
msimons@star:~/debian$ ./ascript &> /dev/null
msimons@star:~/debian$