Results 1 to 7 of 7
Hello everyone,
A bash question.
Why does
Code:
sudo echo -e "echo" > /etc/apt/sources.list.d/echo.txt
returns
Code:
bash: /etc/apt/sources.list.d/echo.txt: Permission denied
?
everything works well after a
Code:
sudo su
.
...
- 09-17-2009 #1Just Joined!
- Join Date
- Sep 2009
- Posts
- 32
sudo echo"xxx" > e.txt
Hello everyone,
A bash question.
Why doesreturnsCode:sudo echo -e "echo" > /etc/apt/sources.list.d/echo.txt
?Code:bash: /etc/apt/sources.list.d/echo.txt: Permission denied

everything works well after a.Code:sudo su
I understand that the sudo operator does not include the redirected output. Could anyone explain that, or point toward some online resource?
Thanks in advance.
- 09-17-2009 #2
It doesn't work because there are two parts to it, and "sudo" is only applying to the first part. You need to use the tee command.
Code:echo echo | sudo tee -a /etc/apt/sources.list.d/echo.txt
- 09-17-2009 #3Just Joined!
- Join Date
- Sep 2009
- Posts
- 32
So one could say that actually the command in question does not have a form of
, but rather something a bit likeCode:sudo (echo xxx > file)
by which I meant to say that it is actually the redirect operator that has the highest priority (much as division has higher priority the addition).Code:>[(sudo echo), file]
Except that > is not a command as such, and it cannot be sudo'ed.
And the tee command you mentioned is, actually, the very '>' as a command.
If that makes any sense
.
Thank you!
- 09-17-2009 #4
The more complete answer is that the shell is running with your user's permissions, and only echo is running with sudo permissions, and it's the shell that handles redirecting the output.
Su works because you're logging in as root, not running just one process with root permissions.
You can also apply sudo to the whole pipeline.
Code:echo "echo text >> /etc/apt/sources.list.d/echo.txt" | sudo sh
I'm using the two >> for append, and one > will overwrite with a new file.Code:sudo sh -c 'echo "text" >> /etc/apt/sources.list.d/echo.txt'
Sudo Manual
- 09-17-2009 #5Just Joined!
- Join Date
- Sep 2009
- Posts
- 32
OK so the | is actually a shell command, and as long as the shell has oridinary perimissions, it can't write /etc/xxxxx. And when I su, the whole shell is getting admin rights.
OK, so that puts the "echo text >> /etc/apt/sources.list.d/echo.txt" as the input for sudo sh. As the command is run with sudo, it does what it wants.Code:echo "echo text >> /etc/apt/sources.list.d/echo.txt" | sudo sh
I understand (after some experimenting likeCode:sudo sh -c 'echo "text" >> /etc/apt/sources.list.d/echo.txt'
and some reading that what the command does is to treat theCode:sh 'ls' sh: Can't open ls sh -c 'ls' debian-testing-i386-netinst.iso ...
the input as the list of commands to be executed (and not the file name as it would without the -c switch).
Thank you very much for the examples and explanations. sh has its quirks which are not evident, though with every small point one gets a part of the big image.
- 09-17-2009 #6The pipe | is connecting the standard output of a command to the standard input of another command. If your first example of sudo echo "echo", you have a command, running as root, that outputs "echo", and then the shell redirects that output and tries to write to a restricted file.OK so the | is actually a shell command, and as long as the shell has oridinary perimissions, it can't write /etc/xxxxx. And when I su, the whole shell is getting admin rights.
sudo sh is opening a root shell. So thisoutputs echo text >> /etc/apt/sources.list.d/echo.txt and plugs that into a root shell. It's like if you had logged into the shell as root with su and typed that line.Code:echo "echo text >> /etc/apt/sources.list.d/echo.txt" | sudo sh
http://en.wikipedia.org/wiki/Pipeline_(Unix)
- 09-17-2009 #7Just Joined!
- Join Date
- Sep 2009
- Posts
- 32


Reply With Quote

