Duplicating stdout to stderr Duplicating stdout to stderr bash bash

Duplicating stdout to stderr


Use tee with /dev/stderr:

echo "FooBar" | tee /dev/stderr

or use awk/perl/python to manually do the replication:

echo "FooBar" | awk '{print;print > "/dev/stderr"}'echo "FooBar" | perl -pe "print STDERR, $_;"


Use process substitution: http://tldp.org/LDP/abs/html/process-sub.html

echo "FooBar" | tee >(cat >&2)

Tee takes a filename as parameter and duplicates output to this file. With process substitution you can use a process instead of filename >(cat) and you can redirect the output from this process to stderr >(cat >&2).


If I may expand @defdefred's answer, for multiple lines I'm using

my_commmand | while read line; do echo $line; echo $line >&2; done

It has the "advantage" of not requiring / calling tee and using built-ins.