Subscribe to How-To Geek

Recommended: Click Here to Run a Free Scan for Common PC Errors   [Sponsored Link]

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:

image

An alternative method would be to just hit the up arrow key, hit either the Home key or Ctrl+A, and then type sudo.

The Geek is the founder of How-To Geek and a geek enthusiast. When he's not coming up with great how-to articles, he's probably writing at his personal blog. This article was written on 07/30/07 and tagged with: Linux

Comments (4)

  1. Oliver

    Thanks.

  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


Leave a Comment




Leave your friendly comment here. If you have a computer help question, leave it on the forums instead.

Note: Your comment may not show up immediately on the site.

Copyright © 2006-2008 HowToGeek.com. All Rights Reserved.