Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell Script: How to write a string to file and to stdout on console?

How to write a string to file and to stdout on console?

If I do

echo "hello" > logfile.txt 

I view only hello in logfile.txt but how can I write hello also on console of Linux?

like image 847
Catanzaro Avatar asked Mar 28 '14 13:03

Catanzaro


People also ask

How do I redirect output to console and file?

Option One: Redirect Output to a File Only To use bash redirection, you run a command, specify the > or >> operator, and then provide the path of a file you want the output redirected to. > redirects the output of a command to a file, replacing the existing contents of the file.

Can we redirect the output of a command to a file and display at the same time?

Redirecting output to Multiple files and screen: If you want to redirect the screen output to multiple files, the only thing you have to do is add the file names at the end of the tee command.


2 Answers

Use the tee command:

echo "hello" | tee logfile.txt 
like image 73
Biffen Avatar answered Oct 15 '22 16:10

Biffen


You can use >> to print in another file.

echo "hello" >> logfile.txt 
like image 24
SuperNova Avatar answered Oct 15 '22 17:10

SuperNova