For example if I want to create a file and enter text in one line I can redirect the output into a file with the use of 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 same command 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
sudo !!
Isn't there a way to "recycle" the last line and just add sudo
in front?
Best Answer
You can't just stick
sudo
in front of a shell command, you have to invoke a shell to evaluate that command again (doing things like expanding variables, opening files for redirection operators, etc.). So that'sexcept that this doesn't quite work, because
!!
interpolates the text of the previous command, special characters and all. You need to retrieve the text of the command as a string and pass that as an argument tosh
. Fortunately, bash'sfc
builtin lets you do that¹.Or even, to be sure to invoke the same version of bash that's currently running:
Note that since the command is executed in a separate shell process, it inherits environment variables (only the ones that
sudo
preserves, mind), but not shell internal variables. Shell options (e.g.kshglob
) and other settings will start from the default either.The same command² works in zsh and ksh, though ATT ksh93 requires both the
first
andlast
number to be passed tofc
³ (which also works in bash, zsh and pdksh/mksh):Using
$0
to designate the executable of the running shell works only if the shell was invoked through the $PATH and the $PATH hasn't changed, or through an absolute path.Here's another method in zsh which is slightly clearer but longer:
A final word of warning:
sudo
is for potentially dangerous commands. Don't make it too easy to use it!¹ There's some extra whitespace at the beginning and the command substitution strips off newlines at the end, but the syntax of the shell doesn't care about that.
² I don't think zsh or ksh have anything like bash's
$BASH
;$0
only works when it's an absolute path, or when it contains no slash and the command search path hasn't changed.³
fc
is an alias forhist
in ATT ksh but that's just as good.