I want to do the following:

$ echo "secrets" > protected_file

But the the problem is, I don't have file permissions for "protected_file", so I try the following:

$ sudo echo "secrets" > protected_file

But that doesn't work either because only the "echo" portion of the command is executed under sudo.

What's the correct way to do this?

-Geoffrey Lee

link|improve this question

feedback

2 Answers

up vote 3 down vote accepted
sudo bash -c 'echo "secrets" > protected_file'
link|improve this answer
feedback

Try echo 'secrets' | sudo tee filename > /dev/null or echo 'secrets' | sudo tee -a filename > /dev/null if you wish to append.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.