[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Redirecting output to stderr
From: |
Bob Proulx |
Subject: |
Re: [Help-bash] Redirecting output to stderr |
Date: |
Sat, 15 Nov 2014 17:32:34 -0700 |
User-agent: |
Mutt/1.5.23 (2014-03-12) |
Marlen Caemmerer wrote:
> I tried to output a echo to stderr.
Like this?
echo foo 1>&2
> Unfortunatelly it does not go to stderr but to stdout.
Then you didn't actually do what you wanted to do. :-(
> 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
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...
> address@hidden:~$ echo "moo" 1>&2
> 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...
> address@hidden:~$ echoerr() { echo "$@" 1>&2; }; echoerr "moo" > /dev/null
> 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...
> 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