Reputation: 3388
I know variations of this question have been asked and answered several times before, but I'm either misunderstanding the solutions, or am trying to do something eccentric. My instinct is that it shouldn't require tee
but maybe I'm completely wrong...
Given a command like this:
sh
echo "hello"
I want to send it to STDERR so that it can be logged/seen on the console, and so that it can be sent to another command. For example, if I run:
sh
echo "hello" SOLUTION>&2 > myfile.txt
(SOLUTION>
being whatever the answer to my problem is)
I want:
hello
to be shown in the console like any other STDERR messagemyfile.txt
to contain hello
Upvotes: 0
Views: 159
Reputation: 780655
There's no need to redirect it to stderr
. Just use tee
to send it to the file while also sending to stdout
, which will go to the terminal.
echo "hello" | tee myfile.txt
If you want to pipe the output to another command without writing it to a file, then you could use
echo "hello" | tee /dev/stderr | other_command
You could also write a shell function that does the equivalent of tee /dev/stderr
:
$ tee_to_stderr() {
while read -r line; do
printf "%s\n" "$line";
printf "%s\n" "$line" >&2
done
}
$ echo "hello" | tee_to_stderr | wc
hello
1 1 6
This doesn't work well with binary output, but since you intend to use this to display text on the terminal that shouldn't be a concern.
Upvotes: 3
Reputation: 361547
tee
copies stdin to the files on its command line, and also to stdout.
echo hello | tee myfile.txt >&2
This will save hello
in myfile.txt
and also print it to stderr.
Upvotes: 1