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

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

Comments:

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

Posted by Jan Spitalnik on March 26, 2009 at 10:05 AM PDT #

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?

Posted by Petr Dvorak on March 26, 2009 at 10:23 AM PDT #

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.

Posted by Petr Dvorak on March 26, 2009 at 10:31 AM PDT #

I find it less confusing to do something like:

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

At least I keep the usual syntax.

Posted by Marc on March 26, 2009 at 03:21 PM PDT #

marc: I updated the blog post - thank you.

Posted by Petr Dvorak on March 26, 2009 at 06:58 PM PDT #

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.

Posted by Fatman on July 01, 2009 at 06:44 PM PDT #

Post a Comment:
Comments are closed for this entry.
Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today