Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
eg.
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...
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.
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
Code:
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.
Second,
Code:
#echo something | wc
1 1 10
seems to imply that echo is indeed printing to stdout.
Maybe you can try
Code:
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)
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.