143

Sometimes when I run a command I don't notice that I needed to run it as super user to have enough permission.

Is there a way to run the same command again but as a super user?

0

10 Answers 10

227

The simplest way is to run:

sudo !!

This will run the last command but as super user.

Source.

10
  • 6
    I found this quite useful and I hadn't come across it before (though it may be common knowledge). I couldn't find it on the site so I thought I would answer it myself.
    – Trufa
    May 27, 2013 at 20:32
  • 34
    As an aside, executing !! on its own runs your previous command as per saved in history. In addition, doing !-1, !-2, and so on executes your last command, second last command, and so on. Handy!
    – oaskamay
    May 27, 2013 at 20:41
  • 9
    As an another aside, !<command> will execute last event from ~/.bash_history that start with <command> May 27, 2013 at 20:54
  • 2
    ctrl-r is also very useful. it will search backwards in your history for commands matching what you type. This will allow you edit the command before running it again
    – KayEss
    May 28, 2013 at 3:37
  • 4
    This stupid ! bash thing copied from tcsh has screwed me up more times than it's ever been useful. Double quotes don't protect from the interpretation of !. $ foo "!bar" -> !bar: event not found.
    – Kaz
    May 28, 2013 at 7:26
85

​​​​​​​​​​​​​​​​​​​​​​​​​​​​​enter image description here

​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​Answer: you enter sudo !! straight afterwards to repeat the previous command with escalated privileges.

edit: Image found via reddit, and it's a parody of the original xkcd comic by Randall Munroe.

1
  • 3
    please do not have a pro-or-against conversation here in comments. If you wish to discuss the merits or otherwise, please use Meta. Thanks.
    – fossfreedom
    May 28, 2013 at 17:42
17

You could try the up-arrow key to scroll through your old commands and rewrite/change them and add the sudo in front of them. (the home button above the arrow keys will set the position to the beginning.)

I dislike the sudo !! idea, because sometimes you add another command in between and don't remember anymore.

7
  • 1
    Fortunately, when you do sudo !!, the shell will expand !! so that you can see what command it's trying to run. So, it may end up being irritating to hit Ctrl-C and find the right command, but it at least won't take you unawares.
    – Reid
    May 27, 2013 at 21:58
  • 4
    @Reid It won't expand until after you hit enter, and if you're within that window where you don't have to enter your password again, it will run without confirmation. +1 to using the history instead of blindly using !!
    – Izkata
    May 28, 2013 at 1:33
  • 5
    @Izkata: I suppose that's true. I use zsh, so I just type sudo !!<TAB> and it autocompletes !! for me. Bash can expand history with M-^ (for me, alt+shift+6), but that's less fluent (you could rebind this, though). There is also shopt -s histverify, which makes it so that when you use history expansion and hit enter, it instead gives you another prompt line with the expansion performed to let you see/edit it.
    – Reid
    May 28, 2013 at 1:46
  • 1
    I was thinking more of the situation where I typed a command, the command returned that permission was denied, and I know I want to run this exact same command again but as sudo, the process of pressing up, home, typing sudo and feels much slower to me. It's the way I have been doing it forever and it just felt inefficient.
    – Trufa
    May 28, 2013 at 9:03
  • @Reid: shopt -s histverify won't help a single bit. Most people will get used to hitting Enter twice. At that point they're still too late to check what happened. May 30, 2013 at 9:29
15

This question could have been generalized into "Run same command but with some modification", since one of the brilliant design decisions in Unix, is that what's implemented once in the shell, can be leveraged by any command being invoked from the shell.

In other words, this trick doesn't apply just to sudo:

  • Repeat the previous command as superuser (sudo !!)
  • Repeat the previous command and time it (time !!)
  • Repeat the previous command with a different environment (env A=b B=z !!)
  • Repeat the previous command (!!)

History expansion (also known as history substitution) is just a shell feature to make repeating previous commands easier (with less keystrokes). Check your shell man page for history expansion. It describes many more tricks and short-cuts you can do with history for example:

You may refer to previous commands by number:

!55                 # repeat command number 55
!-1                 # repeat the previous command, alias for !!
!-2                 # repeat the command before the previous command

You may refer to individual words in previous commands:

some-command !$     # run some-command on the last arg of previous command

You can also search before repeating:

!?awk               # repeat the last command including the string 'awk'

You can search the history and replace something by something else:

!?awk?:s/str1/str2/  # repeat previous command matching 'awk' replacing str1 by str2

And much more, since you can combine command selection, arg-selection, search and replace independently.

The man page of your shell (in this case man bash) is your friend.

1
  • 3
    briliant! that one should be the accepted answer. Jun 14, 2013 at 11:44
14

I'll just add an alternative: as short as typing sudo !! and more flexible:

Up-arrow (once, or until you find the command you want to sudo/edit, if it's older)
ctrl+A   (place cursor at beginning of line)
"sudo "
Enter

It's exactly the same number of keystrokes (I count "ctrl+A" as one...), but adding "up arrow" you can do it on whichever command is in your history.

6
  • 1
    ctrl+A is from the emacs bindings, you can also use the vi bindings (set -o vi). Then you can Escape (go to command mode), and then 'I' (capital 'i', insert at beginning of line). May 30, 2013 at 10:31
  • 1
    @GerhardBurger: I know, but, weirdly, I love vi (especially vim) for multi-line documents editing, but I do much prefer the emacs binding in the shell (having to switch from command to edit mode in the shell doesn't really makes sense, imo, and the shortcuts of emacs are quite enough for everyday use, even if less powerfull) May 30, 2013 at 10:54
  • True, I added it just for completeness, for me it's the same, however I find searching with '/' very convenient ;) May 30, 2013 at 11:01
  • @GerhardBurger: about vi mode's "/", regular (emacs binding) bash has also a very handy way to find previous commands by typing a few letters then 'ctrl+r' until you find it Jun 13, 2013 at 8:27
  • Why not just use "home"? Then it actually is just one keystroke. Oct 22, 2019 at 16:52
7

There are a few ways to do this

  • Simply enter the command again adding sudo before the command

  • Press Up arrow to get the last command and put sudo in front of it.

  • Enter sudo !!

The !! expands to the last entered command. You can do similar things with other things with the command line history see here

6

I use zsh instead of bash and have this in my ~/.zshrc, so pressing Alt+S inserts sudo at the beginning of the command line:

insert_sudo () { zle beginning-of-line; zle -U "sudo " }
zle -N insert-sudo insert_sudo
bindkey "^[s" insert-sudo

Unfortunately, I couldn't track down a way to do the same thing in bash.

1
  • 4
    You can easily do the same in Bash using GNU Readline: add to your ~/.inputrc "\es":"\C-asudo " and you will move to the start of a long line and sudo will be inserted at the start of the line upon pressing alt-s. Restart terminal or re-read ~/.inputrc for it to work.
    – user76204
    May 28, 2013 at 17:25
3

If you want to do it in an alias, you have to use alias redo='sudo $(history -p !!)' for some reason.

2

The way I prefer it:

  • Arrow key up
  • Home
  • "sudo "
  • Enter

But it might be easier to just do "sudo !!", it's up to you.

1
su -c "!!"

If you do not have permission to be root with sudo or sudo program did not install on system.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .