-   Linux - General (
-   -   How do I echo to both standard out and to a file? (

Br. Nicholas 10-11-2004 01:06 AM

How do I echo to both standard out and to a file?
Does anyone know of a way to echo to both standard out and to a file at the same time?

I'm writing a script, and I'd like to echo text to both standard out and append it to a log file, preferably at the same time. Is there any way I can do that in one command?

I've tried things like
echo "test echo" 2>> logfile
and a few things with piping |

But nothing seems to work.

I'd greatly appreciate any help anyone can give me.

With kind regards,

Peter, hieromonk

mirradric 10-11-2004 01:19 AM


man tee

Br. Nicholas 10-11-2004 08:50 AM

Thank you, but unfortunately, this doesn't seem to work with the echo command. "tee" works fine with things like the "date" command, but if you try to use it with the "echo" command, it just gets echoed along with everything else.


echo 'test echo' tee -a log.file

echoes "test echo tee -a log.file" to stdout, and writes nothing in the log file.

echo 'test echo' | tee -a log.file

echoes "test echo" to stdout, and writes nothing in the log file.

So how do I get this to work with echo? And yes, I've already looked at "man echo", and it didn't help. I *always* search before I ask...

Again, I hope someone can help me.


mirradric 10-11-2004 08:56 AM

hmm... It works for me...

mirradric 10-11-2004 09:08 AM

Re: How do I echo to both standard out and to a file?
If it's printing to stdout then it is piping and tee is getting the pipe*. Somehow it's not writing it to the file. Do you have write permission to the file? But it works for other commands.... I have no idea.

edit: *The other posibility is that the pipe is entirely broken.

mirradric 10-11-2004 09:13 AM

could you have by some chance permanently redirected echo's stdout to stderr.

say do you have

alias echo="echo >&2"

somewhere? I tried this and I got your result.

mirradric 10-11-2004 10:24 AM

Maybe you can test using

echo something | wc
if your output is

0 0 0

then your echo is print to stderr instead of stdout

Br. Nicholas 10-12-2004 12:45 AM

Please forgive me, but I got called away... unfortunately for the day. But with both bosses away now, that shouldn't happen again for a while.

mirradric -- exactly what worked for you?

Yes, I have permission for the file. I'm running as root.

How would I know if the pipe is broken? And how would I fix it?

How would I know if I have alias echo="echo >&2" somewhere? Where would I need to look?

echo something | wc gives me
1 1 10

What does that mean?

I really appreciate your help, by the way. :)

Br. Nicholas 10-12-2004 12:57 AM

BTW, I don't know if it is related, but I have another script that I run with a cron job which always gives me this error at the end:

stderr is not a tty - where are you?

Any ideas?

I'm running slack 9.1, if it matters.

mirradric 10-12-2004 01:05 AM


Originally posted by Br. Nicholas
echo something | wc gives me
1 1 10

What does that mean?
It means that the output from echo is indeed piping to wc and the pipe works fine.


mirradric -- exactly what worked for you?
I'm saying that

echo something | tee -a logfile
works for me.

Let me try to think of something...

mirradric 10-12-2004 01:42 AM

I'm at a lost, but let's try to diagnose and narrow down the problem. Something might pop up along the way.

First, you mentioned that

date | tee -a logfile
works, so the problem is not likely to be either tee or bash's pipes. Maybe you can try other programs and see if they work as well. Anyway so far the problem seems to be with echo.


#echo something | wc
1 1 10

seems to imply that echo is indeed printing to stdout.

Maybe you can try

alias echo
to see if any alias for echo have been defined.

Also check that you get the following results (which I think you'll get based on the wc test)

#echo something > filea
#echo something 2> fileb
#cat filea
#cat fileb

Does echo something | tee -a logfile work as a normal user but not as root?
BTW, I'm not implying that you run the commands as root by using # as the shell prompt. Run them as whichever acct that gives you the problem.

got to go for tutorial. will check back with you if I think of anything else.

Br. Nicholas 10-12-2004 03:09 PM

I thought I tried this (echo something | tee -a logfile
), but apparently I must not have. I was very tired last night when I finally was able to look at this problem again, so maybe I didn't. But this seems to work fine.

Thank you very, very much. I was able to cut about 50 lines of code out of this script, and it makes it a lot easier to understand.

I appreciate all of your help very, very much.

With very kind regards,

Peter, hieromonk

All times are GMT -5. The time now is 04:22 PM.