The echo does not need to be executed by root, but the utility that actually writes to the file needs to be executed as root (or as whatever user owns the file) if it's not owned by the current user.Īnother possibility would be to use sudo sh -c 'echo hello >file' To append to a root-owned file, use echo hello | sudo tee -a file This is because the redirection is processed before the command is executed (see above). Won't work if file isn't writable by the current user. The next thing to be aware of, which you hinted at in the end of the question, is that sudo echo hello >file This would execute long_running_thing and it would run to the end, but no output would be saved into file if it wasn't writable (and the output would additionally be written to the terminal from tee). This would not even start long_running_thing if file was not writable. This could be significant in the case where you run some long running process that produces output: long_running_thing >file This would not happen in the first command.Īnother difference is that if the file can not be written to, then the first command, with the redirection, would not even run the echo, whereas the echo would run in the second command, but tee would fail in writing to the file ( tee would still produce text on the terminal though). The tee command would additionally produce output on its standard output, showing the text that would also be appended to the file. There's no difference in the sense that the data in the file will be the same if echo and tee are executed successfully and if the file is writable by the current user.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |