Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicating stdout to stderr

Tags:

redirect

bash

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?

like image 935
Cristiano Paris Avatar asked Jun 29 '10 14:06

Cristiano Paris


2 Answers

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, $_;" 
like image 195
marco Avatar answered Sep 23 '22 16:09

marco


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).

like image 44
brablc Avatar answered Sep 20 '22 16:09

brablc