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
Share:
136,105
Catanzaro
Author by

Catanzaro

Updated on July 05, 2022

Comments

  • Catanzaro
    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 in logfile.txt but how can I write hello also on console of Linux?

  • augurar
    augurar over 9 years
    You can also do tee logfile.txt <<<"hello"
  • Dmitry Tokarev
    Dmitry Tokarev over 6 years
    Just FYI echo adds newline character to the output file. If you don't want it use printf command it only writes the string to file and will not append newline character