I often forget to run commands with sudo
. I'm looking for a way to make a bash function (or alias) for repeating the last command with sudo
. Something like:
S() { sudo $(history 1) }
Any ideas?
Just type: $ sudo !! Bash will expand the two exclamation points to the previous command and it will be run through sudo .
To get the same effect as using the up arrow once, which is showing the last command run without executing it, you can use CTRL+P. Or change the number, and run any of your last commands. For example changing the 1 to a 2 will run the second from last command. This is all made possible by using bash history.
This keyboard has no arrow keys, and the only way I know how to get this kind of behaviour is by pressing Ctrl + R and beginning to repeat my previous command. Is there an easy way to emulate Up + Enter in an UNIX terminal without the arrow keys? You should replace the terminal tag with the shell that you use.
You can write:
sudo !!
(See §9.3 "History Expansion" in the Bash Reference Manual.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With