20

Is there any difference beteween doing:

echo "hahaha" >> file1

and

echo "hahaha" |tee -a file1

?

Yes, I noticed that I cannot write to write protected files even aith sudo echo, but I can if I sudo tee. Thanks.

iamAguest
  • 483
  • 1
  • 6
  • 17
  • 1
    More i/o usage, more memory usage, more cpu cycles, more characters in terminal. – Ipor Sircer Aug 24 '18 at 14:24
  • 1
    tee starts an extra process and means you get a copy of stdout as well. It also lets you use `sudo` to write to a file you don't have write permission on. – Mikel Aug 24 '18 at 14:24
  • to use ```echo``` with ```sudo``` use this syntax: ```sudo /usr/bin/bash -c 'echo "hehe" > file.ext'``` – Sam Sirry Apr 08 '21 at 12:29

2 Answers2

28

There's no difference in the sense that the data in the file will be the same if echo and tee are executed successfully and if the file is writable by the current user.

The tee command would additionally produce output on its standard output, showing the text that would also be appended to the file. This would not happen in the first command.

Another difference is that if the file can not be written to, then the first command, with the redirection, would not even run the echo, whereas the echo would run in the second command, but tee would fail in writing to the file (tee would still produce text on the terminal though).

This could be significant in the case where you run some long running process that produces output:

long_running_thing >>file

This would not even start long_running_thing if file was not writable.

long_running_thing | tee -a file

This would execute long_running_thing and it would run to the end, but no output would be saved into file if it wasn't writable (and the output would additionally be written to the terminal from tee).

The next thing to be aware of, which you hinted at in the end of the question, is that

sudo echo hello >>file

won't work if file isn't writable by the current user. This is because the redirection is processed before the command is executed (see above).

To append to a root-owned file, use

echo hello | sudo tee -a file

Here, we run tee as root. The echo does not need to be executed by root, but the utility that actually writes to the file needs to be executed as root (or as whatever user owns the file) if it's not owned by the current user.

Another possibility would be to use

sudo sh -c 'echo hello >>file'

or

echo hello | sudo sh -c 'cat >>file'

This would use a redirection to append data to the file, but in this case, the shell that performs the redirection is running as root, so it would not fail in appending/creating the file due to restrictive permissions/ownership (it may still fail if e.g. file is the name of a directory).

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
11

Tee is primarily used to redirect the output to multiple files instead of invoking a copy command separately.

E.g.:

wc -l | tee -a file1.txt file2.txt file3.txt

You can elevate privilege to the tee command alone instead of the whole process, where as >> is initiated even before elevated privilege kicks in.

bvargo
  • 135
  • 5
yoga
  • 211
  • 2
  • 2