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.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 07/30/07

Comments (11)

  1. Oliver


  2. Thomas

    Thanks you very much, this is very usefull.

  3. Nep'n

    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

  4. lachlan

    the !! also works with other commands like man

  5. arch

    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?

  6. A554551N

    I’ve been working on heavily modding my Ubuntu desktop, and whatever I google, 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.

  7. blu

    great, i love this one :)

  8. lowell

    no emacs love, huh? ;)

  9. Kalamadea

    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.

  10. Sam

    Yes!!! This is one of the most useful things I have ever heard

  11. Samik

    Thanks! This would help me a lot.

More Articles You Might Like

Enter Your Email Here to Get Access for Free:

Go check your email!