X

"sudo echo" does not work together in Ubuntu (another "waste of time issue")

Guest Author

Sometimes, you need to deal with a really time-wasting issue. I think it
happens even more often if you work in the IT industry. You know the
resolution must be simple, but all your attempts failed. So you try to
search the web, looking for a plain sentence that will lead you out of
that mess...

If you want to write some text to a file that needs a root privileges, you do it this way:

echo "Text I want to write" | sudo tee /path/to/file > /dev/null

or (updated after reading discussion below):

sudo sh -c 'echo "Text I want to write" > /path/to/file'

If you just want to append some text, you do it this way:

echo "Text I want to write" | sudo tee -a /path/to/file > /dev/null

or (updated after reading discussion below):

sudo sh -c 'echo "Text I want to write" >> /path/to/file'

This won't work:

sudo echo "Text I want to write" > /path/to/file

Join the discussion

Comments ( 6 )
  • Jan Spitalnik Thursday, March 26, 2009

    Hey Petr,

    this can't work since you're using a shell redirection to write to a file. Since you're raising the priviledges only to the commands piping the text but not the actual shell that is handling the redirection (and thus the file write), the last line can never work.

    {sudo echo "Text I want to write"} {> /path/to/file}

    \^-- echo has raised priv \^-- shell is actually handling this

    HTH,

    Spity


  • Petr Dvorak Thursday, March 26, 2009

    Yes, but I believe this is written in the post??? I said it will not work... this is why the last line is in red color and is introduced by text "This won't work:"...

    Did I miss something?


  • Petr Dvorak Thursday, March 26, 2009

    Well - maybe I understand where the problem is...

    The intention of those short posts in "time-wasters" category of my blog is to write a "solution without an explanation", for those who are desperate when solving some problem. Short, simple, direct...

    The reason is that I frequently find myself wondering: "Why the H3LL this does not work?" and then, after a relatively long time filled by failed attempts, I find I am a retard (like in this case) and that I do something totally stupid (raising privileges to echo only, not to the redirection).

    So now I understood your post, Jan... I am sorry, it was my fault. Explanation in comment is always a beneficial for the blog post... Thank you for it.


  • Marc Thursday, March 26, 2009

    I find it less confusing to do something like:

    sudo sh -c 'echo "text" > file'

    At least I keep the usual syntax.


  • Petr Dvorak Friday, March 27, 2009

    marc: I updated the blog post - thank you.


  • Fatman Thursday, July 2, 2009

    Thanks Petr, this tripped me too. It would be nice if we could have a sort of "propagate privileges pipe", but I guess it will never happen.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.