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 00:51:41 +0100

On Sat, 15 Nov 2014 22:31:34 +0100 (CET), Marlen Caemmerer
<address@hidden> wrote:

> Hello,
> 
> I tried to output a echo to stderr.
> Unfortunatelly it does not go to stderr but to stdout.
> Only when I use it in a function it works.
> When I put the first statement into a script it works as expected, too.
> 
> It works in zsh though and I guess it worked in 2003 in bash, too.
> Here is my shell output:
> 
> address@hidden:~$
> address@hidden:~$ echo "moo" 1>&2 > /dev/null

This works because redirections are applied left to right, so the first one
redirects fd 1 (stdout) to fd 2 (stderr), but the second one overrides that
and redirects fd 1 (stdout) to /dev/null, which is what remains active when
the command runs.

> address@hidden:~$ echo "moo" 1>&2
> moo

Here, fd1 is redirected to fd 2, without further actions.

> 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.

So you should redirect stderr, eg

echoerr "moo" 2>/dev/null

to suppress the output from the function.

-- 
D.



reply via email to

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