3

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

geofflee
  • 196
  • 1
  • 2
  • 10

2 Answers2

5
sudo bash -c 'echo "secrets" > protected_file'
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
4

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

Zoredache
  • 130,897
  • 41
  • 276
  • 420