How-To Geek
Add Sudo to Your Last Bash Command With "!!" Syntax
How often have you typed in a command in your linux shell, and then realized that you forgot to type sudo, so you end up with an error or editing the dreaded read-only file? This happens to me much more than I’d like to admit, so I’m writing about it.
You have a couple of options, the easiest one is to just use !! to tell bash to use the last command.
For instance, if you tried to type this, you’d get an error that you are editing a read-only file:
vi /etc/apt/sources.list
Once that happens, then you can just use this command:
sudo !!
Which bash will then expand into this command:
sudo vi /etc/apt/sources.list
Screenshot of this in action:
An alternative method would be to just hit the up arrow key, hit either the Home key or Ctrl+A, and then type sudo.
Got Feedback? Join the discussion at discuss.howtogeek.com
Comments (11)
Programmer by day, geek by night, The Geek, also known as Lowell Heddings, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on Google+ if you'd like.
- Published 07/30/07




Thanks.
Thanks you very much, this is very usefull.
That is surprisingly useful, I like the way you give us more than one way to do a lot of these things, the alternative methods can be useful for other stuff too
the !! also works with other commands like man
Just wondering if it would be possible to create a script that would execute this or an alias in ~/.bashrc or something. I’m super-lazy so “sudo !!” is too many characters to type. Why not something like “sss” The issue is that when I try a script or alias, it doesn’t execute the previous command. Any ideas?
I’ve been working on heavily modding my Ubuntu desktop, and whatever I google, howtogeek.com comes up as a top result. Your suggestions, tips, and tricks are spot-on and incredibly useful, and I just wanted to say thank you.
great, i love this one :)
no emacs love, huh? ;)
This is so useful – thanks a lot. It’s *SO* annoying to have go all the way to the beginning of a command to add sudo after you forgot to the first time.
Yes!!! This is one of the most useful things I have ever heard
Thanks! This would help me a lot.