Sunday, January 11, 2009

sudo echo

Many people complain that they cant use sudo with echo.
For example people try to do
sudo echo 1 > /etc/file
To know why this fails, lets break the command in two parts.

sudo echo 1
You are just echoing "1".

> /etc/file
You are trying to write something to file inside /etc for which you need root access. But there is no root access as sudo was needlessly used with echo command.

So to use sudo and echo we need help from one command called tee. With tee you can accomplish the above work.
echo 1 | sudo tee /etc/file
echo 1 | sudo tee -a /etc/file

0 comments:

Post a Comment