Why can't I echo contents into a new file as sudo? [duplicate] Why can't I echo contents into a new file as sudo? [duplicate] bash bash

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'


The answer is to use "tee" with a pipe, a command I wasn't familiar with, so you can use sudo for the second half:

$ echo "tweedle ALL=(ALL) ALL" | sudo tee /etc/sudoers.d/tweedle