I am on centos and I did sudo echo 'testline'>>/etc/test/test it said -bash: /etc/test/test: Permission denied
However, when I do sudo vi /etc/test/test and insert testline and do :wq it writes out fine, why is this happening?
Advertisement
Answer
You need to wrap the whole statement (including the redirect) into a group so the sudo extends around it.
sudo bash -c "echo 'testline' >> /etc/test/test"
Note: that, too, will fail if /etc/test doesn’t exist yet.