Skip to content

Damn Useful: When You Forget to type Sudo

(This post is part of a series.  To read more tips and to learn about the philosophy behind these posts, check out the parent post.)

You know you’ve done this way too many times:

ben@lispclub ~ $ apt-get install blahblahblah
E: Could not open lock file /var/lib/dpkg/lock – open (13 Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

Bah.

You forgot to type sudo first.

So what do you do?  Hit up arrow, swing back to the beginning of the line, type sudo–ARGGH…THE WASTED SECONDS!

Do this instead: sudo !!

In bash, !! means “repeat the last command I entered.”  Bash substitutes your apt-get command after the sudo, and you finally get to install blahblahblah 0.0.13.  Note that this isn’t specific to sudo.  If you do an ls, and then wish you’d grepped the results for foo, type !! | grep foo.

How to remember it:

Imagine an angry father yelling for his son.  “Sudo!!”  Sudo runs down from his room, sees the command  he was supposed to be in front of, and executes it immediately.

14 Comments

  1. oh dang wrote:

    Learn bash, man.
    Like Ctrl+a

    Tuesday, December 9, 2008 at 9:20 am | Permalink
  2. Horton wrote:

    If you suggest an alternative it would helpful if you explained it.

    Tuesday, December 9, 2008 at 9:33 am | Permalink
  3. pete wrote:

    ‘sudo’, Space, Bang, Bang: seven keystrokes
    Up Arrow, Ctrl-A, ‘sudo’, Space: seven keystrokes

    Tuesday, December 9, 2008 at 9:50 am | Permalink
  4. Ben Orenstein wrote:

    pete: Yes, that’s the same number of keystrokes, but this is a “teach a man to fish” sort of thing, and I think you’re too fixated on this one example. You can use !! in a lot of different scenarios where it’s going to save you more typing.

    Tuesday, December 9, 2008 at 10:13 am | Permalink
  5. dude wrote:

    yea the ctrl-p, ctrl-a, sudo, space is what i usually do, but this is handy.

    also, edit your .profile (or similar) and add lines like:

    alias apt=’sudo aptitude’
    alias svim=’sudo vim’
    alias gem=’sudo gem’

    and so on. very handy because then you save all 7 keystrokes :)

    Tuesday, December 9, 2008 at 10:13 am | Permalink
  6. Timothy Andrew wrote:

    Thank you!

    Tuesday, December 9, 2008 at 10:46 am | Permalink
  7. Alan wrote:

    Here’s a bash function that will either pass its arguments to sudo, or if there are no arguments, pass the previous command to sudo:

    function s {
    if [ $# -gt 0 ]
    then
    sudo $@
    else
    sudo $(fc -ln | tail -n1)
    fi
    }

    Tuesday, December 9, 2008 at 11:37 am | Permalink
  8. Nico wrote:

    How could I avoid entering the password every time?

    Tuesday, December 9, 2008 at 3:29 pm | Permalink
  9. Ben Orenstein wrote:

    Nico — this should help: http://sial.org/howto/openssh/publickey-auth/

    Tuesday, December 9, 2008 at 3:34 pm | Permalink
  10. Ben Orenstein wrote:

    Timothy — my pleasure!

    Tuesday, December 9, 2008 at 3:34 pm | Permalink
  11. Ben Orenstein wrote:

    Alan — Very nice. I might have to write that up later.

    Tuesday, December 9, 2008 at 3:35 pm | Permalink
  12. Marco wrote:

    Thanks for all the tips. I like that !! think and the cartoon :)

    Tuesday, December 9, 2008 at 3:36 pm | Permalink
  13. Bootdisk wrote:

    bah… just login as root and be done with it

    j/k :p

    Wednesday, December 10, 2008 at 12:40 am | Permalink
  14. Justin wrote:

    Yes well, thanks for the tip – One of those things many of us probably “know” but never use.

    I’m not sure whether or not the xkcd cartoon would have been improved if it had been used:

    Make me a sandwhich.

    What? Make it yourself.

    Sudo !!

    Okay.

    Less funny I think.

    Wednesday, December 10, 2008 at 1:17 am | Permalink

2 Trackbacks/Pingbacks

  1. another useful Bash skill « Arnold Funken on the Social Web on Tuesday, December 9, 2008 at 3:40 pm

    [...] However, if you want to prepend the previous command with some other command, say man, watch or that sudo (Codeulate), appending  !! still does not save you any keystrokes. However, you still may be [...]

  2. Brain Drippings » Blog Archive » bash function, FTW on Wednesday, December 10, 2008 at 12:10 am

    [...] to Leo Laporte’s twitter post of http://codeulate.com/?p=22 I found this wonderful function buried in the comments. [...]

Post a Comment

Your email is never published nor shared.