I ran this code:

sudo cat <<EOF | sudo sed -e "s,%,$,g" >/etc/init.d/dropbox
  echo "Hello World"

But even though, I get "permission denied", cause you have to be root to make changes in the /etc/init.d directory. And somehow, my command above doesn't work.

Is there a way to solve this?

migrated from stackoverflow.com Oct 21 '10 at 7:45

This question came from our site for professional and enthusiast programmers.

The redirection to a file is handled by bash. It does therefore not inherit permissions granted by sudo.

Use sudo tee for writing to a file as root.

Try this:

cat | sed -e 's,%,$,g' | sudo tee /etc/init.d/dropbox << EOF
  echo "Hello World"

Notice that $, inside double quotes might be interpreted.

  • It still gives me "Permission denied". – 8k_of_power Oct 21 '10 at 6:14
  • I have edited the answer. use sudo tee rather. – Benoit Oct 21 '10 at 6:16

You could grant yourself persistent su-rights with
# sudo -s

then your command (do not need to sudo anymore) and exit with
# exit

I assume you're asking Ubuntu-related because your question is tagged with that. In other distribution like Suse you'll have the ability to use
# su
instead of # sudo -s

  • Or get persistent su-rights with just su. – Chetan Oct 21 '10 at 6:09
  • 1
    @Chetan: This does not work in Ubuntu. (The question is tagged with Ubuntu, so I expect that it was in this consense) – MOnsDaR Oct 21 '10 at 6:11
  • sorry, I forgot to mention that this is in a script so I can't do sudo to run it. – 8k_of_power Oct 21 '10 at 6:13
  • If its in a script you could start the script with sudo as far as I know: # sudo ./myScript – MOnsDaR Oct 21 '10 at 6:14
  • But since it's a script I cannot use sudo -s inside the script cause the lines after it won't run. – 8k_of_power Oct 21 '10 at 6:17

This part: sudo sed -e "s,%,$,g" >/etc/init.d/dropbox cen be viewed as this:

sudo somecommand --put the result of the sudo command into--> /etc/init.d/dropbox

In the same way as this:

ls somedirectory > filename
ls somedirectory --put the result of the ls command into--> filename

This means that the file writing will be done as your current user, not as root.

You can solve it by using tee as Benoit shows in his answer.

  • Thanks for the description of the problem. But how do I solve it? – 8k_of_power Oct 21 '10 at 6:16

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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