Depends on how much access your administrator has given you via sudo. The simplest answer, assuming that you have permission to do so, is to run "sudo -s" to get a privileged shell and then just do your
echo somestring >> test.txt
as 'normal'. If you need to do it automatedly:
sudo /bin/sh -c 'echo somestring >> test.txt'
The reason that what you have won't work (other than the fact that you left what I assume to be an "echo" command out) is that the file redirection happens in the context of your shell and the sudo only applies to the command you told it to run.
The problem in your original try is that the ">>" is happening in the shell running as you; bash opens the file, not whatever is being run by sudo. In my version, you're passing the whole command including the redirection to a bash run by sudo.
Depends on how much access your administrator has given you via sudo. The simplest answer, assuming that you have permission to do so, is to run "sudo -s" to get a privileged shell and then just do your
as 'normal'. If you need to do it automatedly:
The reason that what you have won't work (other than the fact that you left what I assume to be an "echo" command out) is that the file redirection happens in the context of your shell and the sudo only applies to the command you told it to run.
The problem in your original try is that the ">>" is happening in the shell running as you; bash opens the file, not whatever is being run by sudo. In my version, you're passing the whole command including the redirection to a bash run by sudo.
You can try ex-way:
It's simple in-place file editing and it's useful in scripts, so you don't have to do any shell piping.
Lots of people recommend
tee
for this, but I don't like the side-effect of writing tostdout
. An alternative iscat
:If
cat
had an argument liketee
's-a
flag, that would be a little handier:But unfortunately it doesn't.