Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat last command with "sudo"

Tags:

bash

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?

like image 843
Nemo Avatar asked Jun 21 '13 23:06

Nemo


People also ask

How do I run a previous command as sudo in Linux?

Just type: $ sudo !! Bash will expand the two exclamation points to the previous command and it will be run through sudo .

How do you repeat the last command in bash?

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.

Which command is used to repeat the last command Unix?

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.


1 Answers

You can write:

sudo !! 

(See §9.3 "History Expansion" in the Bash Reference Manual.)

like image 183
ruakh Avatar answered Sep 22 '22 02:09

ruakh