How to pipe stderr to a command in Bourne or Korn shell

Henk M. Keller henk at atcmp.nl
Tue Oct 9 19:46:34 AEST 1990


In article <1990Oct8.165133.17187 at cti-software.nl>, pim at cti-software.nl (Pim Zandbergen) writes:
> 
> How can one redirect stderr to a command while leaving stdout unaffected ?

The real solution in sh and ksh is to exchange stdout and stderr:

	command_1 3>&1 1>&2 2>&3 3>&- | command_2

Well, some comments may be needed :-)
   (a)	3>&1	File descriptor 3 now is an 'alias' for 1 (original stdout)
   (b)	1>&2	File descr. 1 now is an 'alias' for 2 (original stderr)
   (c)	2>&3	File descr. 2 now is an 'alias' for 3 which still is an alias
		for the original stdout
   (d)	3>&-	File descr. 3 is closed as it is no longer needed

Net effect:
	File descriptor 1 (stdout) is connected to the original stderr (b)
	File descriptor 2 (stderr) is connected to the original stdout (c)

For C writers amongst you:
	3>&1 will probably be implemented as
		close(3);	/* To ensure 3 is free */
		fcntl(1, F_DUPFD, 3);	/* Returns lowest possible   */
					/* 'dup'ed file descr. >= 3  */


  /-------------------------------------------------------+
 /  Henk Keller                         AT Computing bv   |
|   Teleph.: +31 80 566880              P.O. Box 1428     |
|   Fax:     +31 80 555887              6501 BK  Nijmegen |
|   Email:   henk at atcmp.nl              The Netherlands   |
|            mcsun!hp4nl!kunivv1!atcmpe!henk              |
|                                                         |
|                    -- do not fold --                    |
+---------------------------------------------------------+



More information about the Comp.unix.shell mailing list