Menu

Toshimaru's Blog

Writing to a file that requires sudo in one line

The error

The following command does not work:

$ sudo echo test >> /file/thare/requires/sudo
bash: /file/thare/requires/sudo: Permission denied

The fixed command

Use sudo tee -a instead.

$ echo test | sudo tee -a /file/thare/requires/sudo

For example:

$ echo '127.0.0.1 localhost.example.com' | sudo tee -a /etc/hosts
Load more