It is easy to redirect stdout or stderr to the same output (file or one of the 2 std output) with a 1>&1,>&2 and/or >file. Is there a way to send the same output to both the std output in KSH (like | tee File but with &2 as file) ? I try to have the stream to stdout AND on stderr at the same time (duplicate the output, one on each channel) but without using temporary object (variable or file)

I use a temporary variable but try to avoid this

   #all my code with output to stdout and stderr
echo "${Tempo}"
echo "${Tempo}" >&2

Question asked on StackOverflow with suggestion to ask it here


If your system doesn't have /dev/stderr, you can use perl that way:

} | perl -pe 'print STDERR'

perl processes the input one line at a time, so you won't see partial lines there. For instance with code like:

printf 'Foo'; sleep 2; printf 'Bar\n'

You'll only see FooBar after 2 seconds.

Also note that some commands buffer their output when it doesn't go to a terminal, so that may affect the way the output is done.

  • KSH/AIX does not accept /dev/stderr if not available awk: 0602-575 Cannot find or open file /dev/stderr. The input line number is 1. The source line number is 1.. Same problem as a direct redirection in this case. – NeronLeVelu Nov 11 '14 at 6:30
  • 1
    Sorry, for some reason I thought to remember that was a POSIX requirement, but it's not. I switched to perl which is going to be more reliable anyway as it will support NUL bytes in the input. – Stéphane Chazelas Nov 11 '14 at 7:40
  • yep, with perl it works and I keep the 2 channel for any following action if needed – NeronLeVelu Nov 11 '14 at 8:59

I don't know whether ksh has a feature to do that. zsh can do it (with the multios feature).

But there is a way with tee:

echo foo | tee /dev/stderr
echo foo | tee /proc/self/fd/2
  • good idea the /dev/stderr but it failed on may KSH (also with -a) – NeronLeVelu Nov 10 '14 at 13:35
  • @NeronLeVelu Interesting. Isn't that Linux? What is the output of ls -l /dev/stderr? Does the second option work? – Hauke Laging Nov 10 '14 at 13:37
  • 2
    On Linux, you want tee -a /dev/stderr as if stderr is going to a file, that file would be truncated otherwise. Other Unices that support /dev/stderr don't have that problem. Note that /proc/self/fd/2 is Linux specific. – Stéphane Chazelas Nov 10 '14 at 14:00
  • ls: 0653-341 The file /dev/stderr does not exist. (AIX /KSH) – NeronLeVelu Nov 10 '14 at 15:00
  • I have a /dev/tty that can be used so echo "hello" | tee -a /dev/tty 1>&2 do the job. The only point seems that nothing can be treated after from stdin (send to tty not streamed) so it "should" be the last instruction (for stdin entry) – NeronLeVelu Nov 10 '14 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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