This question already has an answer here:

Say I have 3 commands linked through pipes:

A | B | C

I would like B to output error messages to stderr and some progress to the terminal. If stderr is the same as the terminal, then that is easy: Simply send the progress information to stderr. But what if stderr is redirected?

A | B 2>/tmp/file | C

Then I still want the progress information to go to the terminal, while error messages go to the file.

Can I now somehow identify the terminal that stderr would have ended up on had it not been redirected?

My initial idea would be to find the tty using tty and then opening that and writing to it. However, if I call tty from inside B tty says: not a tty, which I believe is because of the pipe between A and B.

I would like to do this from Perl.

marked as duplicate by Gilles 'SO- stop being evil', Anthon, Hauke Laging, Ramesh, jasonwryan Dec 14 '14 at 3:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Try to find the parent process's fds. – muru Dec 13 '14 at 11:26
  • You can use a tee programm: tee - read from standard input and write to standard output and files – Costas Dec 13 '14 at 11:35
  • You cannot use tee. tee makes a copy of the stream. That is not the wanted behaviour. – Ole Tange Dec 14 '14 at 6:49
  • 1
    Open /dev/tty and write to it. – Mark Plotnick Dec 14 '14 at 11:09

Linux specific:

echo |perl -e '$p=getppid; `echo foo > /proc/$p/fd/2`'

If you here redirect stderr 'foo' is still printed on the terminal:

echo |perl -e '$p=getppid; `echo foo > /proc/$p/fd/2`' 2>/dev/null

Not the answer you're looking for? Browse other questions tagged or ask your own question.