Path: utzoo!attcan!uunet!ns-mx!iowasp.physics.uiowa.edu!maverick.ksu.ksu.edu!zaphod.mps.ohio-state.edu!samsung!olivea!mintaka! bloom-beacon!eru!hagbard!sunic!mcsun!hp4nl!sci.kun.nl!atcmpe!henk From: henk@atcmp.nl (Henk M. Keller) Newsgroups: comp.unix.shell Subject: Re: How to pipe stderr to a command in Bourne or Korn shell Summary: send stderr to a command, leaving stdout unaffected Message-ID: <668@atcmpe.atcmp.nl> Date: 9 Oct 90 09:46:34 GMT References: <1990Oct8.165133.17187@cti-software.nl> Organization: AT Computing, Nijmegen, The Netherlands Lines: 35 In article <1990Oct8.165133.17187@cti-software.nl>, pim@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@atcmp.nl The Netherlands | | mcsun!hp4nl!kunivv1!atcmpe!henk | | | | -- do not fold -- | +---------------------------------------------------------+