As everyone kowns,The command echo cooperate rederection that is write a shortcut to the file information,this article illustrate how write some information to a file which only root can permit write or read.
example 1 ,normal user write test file.
$ echo "something" > test.asc# or $ echo "something" >> test.asc |
if the test.asc permission set for only root user who have permission to write:
$ sudo chown root.root test.asc |
Then, we use sudo and echo command again to modify permission
$ sudo echo "another something" >> test.asc -bash: test.asc: Permission denied |
At this time ,we can see the bash refuse to do,point out insufficient privileges.
This is because the redirection symbols > and >> is also a bash command,we use sudo just let echo have root permisiion,but did not > and >>. so the bash will think these two commands haven't written perrmission.
There are two kinds of the way solve the problen. The first method is to use the "sh -c"command,which allows the bash to a string as a complete command to execute,so that it can be extanded to the whole range.The specific usage is as follows:
$ sudo sh -c 'echo "another" >> test.asc' |
Another method is to use the pipeline command,the command can read from stardard input information and written to standard output or file,the usage is as follows
$ echo "the third " | sudo tee -a test.asc |