Shell Script: How to write a string to file and to stdout on console?
136,105
Solution 1
Use the tee
command:
echo "hello" | tee logfile.txt
Solution 2
You can use >>
to print in another file.
echo "hello" >> logfile.txt
Author by
Catanzaro
Updated on July 05, 2022Comments
-
Catanzaro almost 2 years
How to write a string to file and to
stdout
on console?If I do
echo "hello" > logfile.txt
I view only
hello
inlogfile.txt
but how can I writehello
also on console of Linux? -
augurar over 9 yearsYou can also do
tee logfile.txt <<<"hello"
-
Dmitry Tokarev over 6 yearsJust FYI
echo
adds newline character to the output file. If you don't want it useprintf
command it only writes the string to file and will not append newline character