Echo into a file with root permissions

Simply prepending sudo to echo will fail since the redirection is executed by the shell, not the command you run.

$ sudo echo "foo" >> /etc/bar
bash: /etc/bar: Permission denied

Shortly, the above is running echo as root, but >> will be run by the surrounding shell.
To overcome this, you can use tee:

$ echo "foo" | sudo tee -a /etc/bar/

To overwrite the file, rather than append to it, omit -a.