Skip to main content
2 of 4
added 226 characters in body
rubo77
  • 30.6k
  • 46
  • 141
  • 217

Redirect the output into a file with sudo

If I want to create a file and enter text in one line I can use the > operator:

echo "something" > /path/foobar

but if I don't have access to the folder /path/ and need sudo priviledges, How can I achieve this as a normal user with sudo rights?

I tried

sudo echo "something" > /path/foobar

but that doesn't work, because the sudo only counts for the edit but not for the right part
of the >

Sure, I could become root before with sudo su or use tee instead:

echo "something" | sudo tee /path/foobar

But I would like to find a solution where I can work with the last line as replacement via !!

Isn't there a way to "recycle" the last line and just add sudo in front?

maybe with an option like

sudo --command='!!'
rubo77
  • 30.6k
  • 46
  • 141
  • 217