Discussion:
[Help-bash] Redirecting output to stderr
Marlen Caemmerer
2014-11-15 21:31:34 UTC
Permalink
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:

***@all:~$
***@all:~$ echo "moo" 1>&2 > /dev/null
***@all:~$ echo "moo" 1>&2
moo
***@all:~$ echoerr() { echo "$@" 1>&2; }; echoerr "moo" > /dev/null
moo
***@all:~$

But I have no idea why it is so and if this is a bug.

Cheers
nosy
--
* Marlen Caemmerer
* Richard-Sorge-Str. 82
monoro * 10249 Berlin
*
* Tel: 0179/733 90 72
USt-ID: DE 252684276
Davide Brini
2014-11-16 00:44:37 UTC
Permalink
Post by Marlen Caemmerer
moo
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.
Bob Proulx
2014-11-16 00:32:34 UTC
Permalink
Post by Marlen Caemmerer
I tried to output a echo to stderr.
Like this?

echo foo 1>&2
Post by Marlen Caemmerer
Unfortunatelly it does not go to stderr but to stdout.
Then you didn't actually do what you wanted to do. :-(
Post by Marlen Caemmerer
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.
That is equivalent to this:

echo "moo" > /dev/null

That is not what you want. Think of it this way:

fd[0] = stdin
fd[1] = stdout
fd[2] = stderr

Thie above is how things are always initialized by the shell before
any command is invoked. Which is why we call it a "redirection" when
we change the value of those file descriptors. We _redirect_ them.

fd[1] = fd[2] # 1>&2 fd[1] now points to the same place as stderr
fd[1] = /dev/null # 1>/dev/null fd[1] now points to /dev/null
...echo moo to fd[1] pointing to /dev/null...
Post by Marlen Caemmerer
moo
Yes. That works. That redirects the stdout of the command to stderr.

fd[1] = fd[2] # 1>&2 fd[1] now points to the same place as stderr
...echo moo to fd[1] pointing to stderr...
Post by Marlen Caemmerer
moo
fd[1] = /dev/null # >/dev/null
...call echoerror...
echoerr_fd[1] = fd[2] # 1>&2 redirects echoerror's fd[1] to stderr
...echo boo to echoerr_fd[1] pointing to stderr...
Post by Marlen Caemmerer
But I have no idea why it is so and if this is a bug.
It is working as expected. There is no bug there.

Bob
Jason Ferrer
2014-11-15 23:06:44 UTC
Permalink
http://mywiki.wooledge.org/BashFAQ/055
Post by Marlen Caemmerer
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.
moo
moo
But I have no idea why it is so and if this is a bug.
Cheers
nosy
--
* Marlen Caemmerer
* Richard-Sorge-Str. 82
monoro * 10249 Berlin
*
* Tel: 0179/733 90 72
USt-ID: DE 252684276
--
"Unfortunately time is always against us"
Loading...