Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Redirecting stderr on Unix/Linux

8 views
Skip to first unread message

Ryan McCoskrie

unread,
Apr 27, 2009, 12:51:00 AM4/27/09
to
I've seen the act redirecting a programs error log mentioned in a couple
of places (including required knowledge for LPI-101) but I haven't been
able to find an explanation in any manuals on the shell or Unix. That
includes books as well as online documentation.

Is this easy in a fairly standard way like stdout redirection or does it
take special commands and/or ceremonies to do?

I'm working on Linux at the moment but if you can give information about
this on other systems as well that would be well appreciated.

TIA.

--
Quote of the login:
Seeing is believing. You wouldn't have seen it if you hadn't believed it.

Barry Margolin

unread,
Apr 27, 2009, 1:20:41 AM4/27/09
to
In article <gt3dk3$n50$1...@news.albasani.net>,
Ryan McCoskrie <ryan.mc...@gmail.com> wrote:

> I've seen the act redirecting a programs error log mentioned in a couple
> of places (including required knowledge for LPI-101) but I haven't been
> able to find an explanation in any manuals on the shell or Unix. That
> includes books as well as online documentation.
>
> Is this easy in a fairly standard way like stdout redirection or does it
> take special commands and/or ceremonies to do?
>
> I'm working on Linux at the moment but if you can give information about
> this on other systems as well that would be well appreciated.

In "man bash" it's in the section titled REDIRECTION. Here's where it
describes redirecting output:

Redirecting Output
Redirection of output causes the file whose name results from
the expansion of word to be opened for writing on file
descriptor n, or the standard output (file descriptor 1) if n is
not specified. If the file does not exist it is created; if it
does exist it is truncated to zero size.

The general format for redirecting output is:

[n]>word

Since stderr is file descriptor 2, the syntax is 2>filename.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

Michael Tosch

unread,
Apr 27, 2009, 3:46:36 AM4/27/09
to
Ryan McCoskrie wrote:
> I've seen the act redirecting a programs error log mentioned in a couple
> of places (including required knowledge for LPI-101) but I haven't been
> able to find an explanation in any manuals on the shell or Unix. That
> includes books as well as online documentation.
>
> Is this easy in a fairly standard way like stdout redirection or does it
> take special commands and/or ceremonies to do?
>
> I'm working on Linux at the moment but if you can give information about
> this on other systems as well that would be well appreciated.
>
> TIA.
>

Yes, there is a ceremony:

man $SHELL

--
echo imhcea\.lophc.tcs.hmo |
sed 's2\(....\)\(.\{5\}\)2\2\122;s1\(.\)\(.\)1\2\11g;1s;\.;::;2'

Stephane CHAZELAS

unread,
Apr 27, 2009, 2:59:25 PM4/27/09
to
2009-04-27, 16:51(+12), Ryan McCoskrie:

> I've seen the act redirecting a programs error log mentioned in a couple
> of places (including required knowledge for LPI-101) but I haven't been
> able to find an explanation in any manuals on the shell or Unix. That
> includes books as well as online documentation.
>
> Is this easy in a fairly standard way like stdout redirection or does it
> take special commands and/or ceremonies to do?
>
> I'm working on Linux at the moment but if you can give information about
> this on other systems as well that would be well appreciated.
[...]

stderr is the file open on file descriptor 2.

So redirecting stderr, is opening whatever you want to redirect
it to on fd 2.

dup2(open(...), 2) for instance.

Most shells also allow to redirect stderr, with Bourne-like
shells:

cmd 2> some-file

or

cmd 2>&4

if you want to redirect to the same resource as that open on fd
4.

With rc like shells,

cmd >[2] some-file
or
cmd >[2=4] some-file

csh like shells only allow to redirect both stdout and stderr at
the same time to some file, and don't support the x>&y syntax.

cmd >& some-file

--
Stᅵphane

0 new messages