views:

57

answers:

3

I'd like to have the stdout of a command replicated to stderr as well under bash. Something like:

$ echo "FooBar" (...)
FooBar
FooBar
$

where (...) is the redirection expression. Is that possible?

+2  A: 

From the command line

script.sh >f.txt 2>&1

or, specific to your output

echo "FooBar" >f.txt 2>&1
KevinDTimm
This is solving a different problem: merging stdout and stderr into a single output stream. What the question asks for is almost the reverse of this: taking the command's single stdout stream and replicating it to both stdout and stderr.
Gordon Davisson
agreed - marco has the correct answer (I toyed with a tee implementation too, but re-reading the question steered me away from there)
KevinDTimm
A: 

For redirecting to stderr, I would use >&2 or >/dev/stderr. For replicating output, I would use tee. The drawback of it is that a temporary file is needed:

echo "FooBar" | tee /tmp/stdout >&2 ; cat /tmp/stdout
mouviciel
+4  A: 

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, $_;"
marco
Nice, that worked flowlessly.
Cristiano Paris