Why can't I echo contents into a new file as sudo? [duplicate]
The echo
command is being run as root
, but the redirection is done by your shell, so it's executed as the current user, not as root
.
The simplest solution is to invoke a root shell to run both the command and the redirection.
Rather than:
sudo echo line > file
try this:
sudo sh -c 'echo line > file'
or
sudo bash -c 'echo line > file'