help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Redirecting output to stderr


From: Davide Brini
Subject: Re: [Help-bash] Redirecting output to stderr
Date: Sun, 16 Nov 2014 01:44:37 +0100

On Sun, 16 Nov 2014 00:51:41 +0100, Davide Brini <address@hidden> wrote:

> > address@hidden:~$ echoerr() { echo "$@" 1>&2; }; echoerr "moo" > /dev/null
> > moo
> > address@hidden:~$
> > 
> > But I have no idea why it is so and if this is a bug.
> 
> You are redirecting fd 1 (stdout) of something (a function, in this case,
> but could be anything) that outputs to fd 2 (stderr). Since fd 2
> itself is not redirected, it is unaffected and goes to its default output,
> that is, the terminal.

I realize this explanation isn't very clear, so let's try to phrase it
differently. In this case, the outer redirection is executed first, so the
function starts out with its fd 1 pointing to /dev/null. But then the
function runs, and in there is a command that overrides the external
redirection, namely the echo 1>&2, so fd 1 is redirected once again, this
time to fd 2 (stderr), and that's the active redirection when the echo runs.

-- 
D.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]