Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 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 md5sums 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?

share|improve this question
    
You are missing sudo in your commands; that will show different results between the 2 ;) –  Rinzwind Jan 20 at 18:50
    
@Rinzwind I'm running these commands as root. –  Alexej Magura Jan 20 at 19:44

2 Answers 2

up vote 37 down vote accepted

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.

share|improve this answer
1  
Excellent answer. –  lazyPower Jan 20 at 20:24
    
You can sudo without tee - sudo sh -c 'echo SOMETHING > FILE'... ;) –  Wilf Jan 20 at 20:50
5  
@wilf yeah, but when you need to output quotes ", things becomes messy –  Braiam Jan 20 at 22:40
1  
In the first case, if you want to write to N files, I would prefer echo "some text" | tee file1 file2 ... fileN and maybe append > /dev/null, if you don't want clutter on stdout. –  Elmar Zander Jan 21 at 9:33
    
Nice one @ElmarZander :) –  Rinzwind Jan 21 at 9:34

First of all, tee itself doesn't append text, nor does >.

It is tee -a and its complement, >> that APPENDS text.

I don't believe all shells support the >> function, so that is why tee is more commonly used. (Think of just plain old sh). Tee is a command, while >> is an operator.

If you use (my personal favorite) bash, > and >> are much nicer/easier.

Using tee also allows you to sudo JUST that command so you don't have to sudo the entire statement, as in sudo sh -c "echo foo > bar". tee also allows you to split the output. Of course, all of this can be seen in man tee. It's mainly just your personal preference.

SRC: http://stackoverflow.com/questions/14538834/whats-the-difference-between-echo-x-y-and-echo-x-tee-y
SRC2: http://unix.stackexchange.com/questions/20469/difference-between-21-output-log-and-21-tee-output-log

share|improve this answer
3  
Clarification on "sudo JUST that command", for anyone finding this a little complex: using a form like sudo somecommand >> filea will run sudo somecommand and then, as the invoking user, append the output to filea. Using sudo sh -c "somecommand >> filea" works, but can cause nested-quoting nightmares. Using somecommand | sudo tee -a filea runs somecommand as the invoking user, and then appends the output to filea as root - which is usually what the user wanted. –  Darael Jan 20 at 18:55
    
@Darael I edited the post. Thank you for making the clarification. –  Whaaaaaat Jan 21 at 3:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.