1

It is OK to use sudo on a single command to run it with superuser privileges:

sudo echo "hi"

But when I use pipes with the command:

sudo echo "hi" > /a.txt

The superuser privilege does not extend to the pipe (> /a.txt) part, and I get an error for denied permission:

bash: /a.txt: Permission denied

My question is:

Is there a syntax in which I can extend the scope of sudo in above to also work on the pipeline part?

0

1 Answer 1

6

Run the command in a root-elevated shell and then your I/O redirection will also be done as root:

sudo bash -c 'echo "hi" > /a.txt'

If you don't want to create a subshell to do this, here's a workaround with tee:

echo "hi" | sudo tee /a.txt > /dev/null

Additional Resources:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .