Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Redirect &2 into a command - HowTo

Helmut Jarausch wrote:
> Sorry if this is a FAQ (I didn't find it there).

You may find it in the general shell FAQs; it'll work in any proper
shell (i.e. not csh, tcsh).

> Is there any possibility to redirect STDERR to a command like 'xless'
> without redirecting  STDOUT as well?

The trick is to use a spare FD to swap the first two.

mycmd 3>&1 1>&2 2>&3 3>&- | xless

which means 1) open a new fd 3 to stdout (the pipe) 2) redirect stdout
to where stderr is 3) now move stderr to 3, which is the original
stdout 4) to be tidy, close the spare fd, though in this case that's
not actually necessary (I won't bore you).

One important extra which isn't in anyone else's FAQ:  in zsh, you need
to `setopt nomultios', since a `multio' will interpret the `1>&2' as
saying you want stdout to go to both the old stderr *and* the pipe,
which in this case you don't.  (Do we need an option `pipemultios', which
is off by default?)

Peter Stephenson <pws@xxxxxx>       Tel: +49 33762 77366
WWW:  http://www.ifh.de/~pws/       Fax: +49 33762 77413
Deutsches Elektronen-Synchrotron --- Institut fuer Hochenergiephysik Zeuthen
DESY-IfH, Platanenallee 6, 15738 Zeuthen, Germany.

Messages sorted by: Reverse Date, Date, Thread, Author