Monday, May 6, 2024
108
rated 0 times [  108] [ 0]  / answers: 1 / hits: 52184  / 1 Year ago, mon, may 15, 2023, 4:08:48

Why is it that almost all instructions regarding appending text to system files like fstab and /etc/apt/sources.list.d/<name>.list involve using tee and echo to append said text?



Take the following examples, which are run as root:



## 1
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee -a file1
## 2
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' >> file2


Running diff -u file1 file2 returns nothing; running md5sum file1 file2 shows their checksums are identical, which brings me back to my original question:



Why is the | tee <FILENAME> so prevalent across Ubuntu docs, is it just good practice, otherwise wouldn't it be easier to just use example 2 instead of passing the output from echo to tee?


More From » command-line

 Answers
4

There is a difference: tee duplicates the output: it sends it both to the file and to the display.



But there is more:




  • For example, if you want to write some string into two files at once, the command with tee you can use is:



     echo "some text" | tee file1 > file2  

  • Another thing tee can help you is to avoid one problem when using sudo. The normal output redirection operator is always executed with your user privileges, also when you write a sudo in front of the command which generates the STDOUT text. In other words, this will fail if you dont have the permission to write to that file:



     sudo echo "something" > bar  


    But with tee, everything will go well:



    echo "something" | sudo tee bar  



2 examples from this site. It has some more.


[#27380] Wednesday, May 17, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
termetalli

Total Points: 326
Total Questions: 127
Total Answers: 110

Location: Sao Tome and Principe
Member since Sat, Sep 12, 2020
4 Years ago
termetalli questions
Sat, Apr 30, 22, 17:54, 2 Years ago
Mon, Dec 6, 21, 05:24, 2 Years ago
Thu, Jun 30, 22, 00:32, 2 Years ago
Mon, Dec 19, 22, 00:15, 1 Year ago
;