How to echo to file with sudo?

bash echo sudo

Often we want to echo some content into file, for which we need root permissions. There is a command tee to help with that. It will display passed string and at the same time write it to file.

Example of trimming log file:

echo "" | sudo tee error.log
To append to a file add -a parameter to tee:
echo "manual" | sudo tee -a mysql.override