Add "sudo" to the Last Command Typed
The Linux utility sudo is an easy way to allow normal users to temporarily have root privileges in order for them to execute a command. In Ubuntu, sudo is used extensively because there is no real root user. In case you are not familiar with sudo, this is how it works. The line below will execute “command” as the root user.
sudo command
Where you run into problems is when you forget to type sudo before the command, which can be very annoying. Luckily, there is an easy way to correct your mistake. On the terminal two exclamation marks represent the last command that was typed. Therefore, the command below will prepend sudo to the previous command that was typed.
sudo !!
And here is another example:
tom@tomstation:~$ whoami
tom
tom@tomstation:~$ sudo !!
sudo whoami
root
tom@tomstation:~$
If you are a regular user of sudo like me, I know that this command will save you some time.
Archived Comments
said, said,
More on that.
!! for last command.
!$ for last argument of last command.
!-2 for second last command.
!:0 for 0th part (i.e. command name) of last command.
!:3 for 3rd part (i.e. 3rd argument) of last command.
!:3-5 for 3rd to 5th parts of last command.
!-2:3-5 for 3rd to 5th parts of second last command.
For more, see EXPANSION section on bash’s manpage.
I always forget what is keyword for last argument, is it !$, is it $!, is it !!? Can’t memorize all the syntax.
Suppose I type the following code and I’m not sure !$ is what I want, so I’m
afraid to press enter, thinking “what if !$ is the whole last command?”
mv *.txt !$
In that case echo is useful.
echo mv *.txt !$
Or add following line to ~/.bashrc :
bind Space:magic-space
Now things like !! and !$ expands automatically after spaces.
iampriteshdesai
You can also try to type the same command again with minimal effort:
First type sudo and then hit the up arrow key. That will print the previous command