64

I am a user of Ubuntu with not much experience and I have been using sudo.

What does sudo !! do and how?

5
  • 7
    Duplicate of, except from the other side: Run same command again but as sudo
    – user
    Nov 18, 2013 at 11:59
  • 6
    @WarrenHill he doesn't want the "how" but the "what" and "how". He ask more about the !! part of the command.
    – Braiam
    Nov 18, 2013 at 15:14
  • 1
    @Nakilon shouldn't be. This question is asking what the !! do and how. The other wants a method to do this.
    – Braiam
    Nov 18, 2013 at 18:14
  • @MichaelKjörling you meant related right?
    – Braiam
    Nov 18, 2013 at 18:14
  • 2
    @Braiam Actually, I did mean duplicate; the two highest-voted answers to that one specifically explain what sudo !! does, and there are two answers that actually specifically explain what !! does.
    – user
    Nov 18, 2013 at 18:50

7 Answers 7

74

!! in bash is an alias for the previous command (see Event Designators). So it re-runs the previous command with sudo permissions.

1
  • 15
    Note: ! commands are usually not available in bash scripts (only in interactive sessions). They can be disabled with set +o histexpand.
    – Benoit
    Nov 18, 2013 at 12:26
64
+50

sudo bang bang is a very useful command when working in Command Line Interface.

Some Linux distros have you login as a user instead of an administrator.

So, to do something admin-wise, you have to proceed the command with sudo (Super-User DO), which tells the system “you will do this, because I said so.” The !! / bang-bang (! = bang) is basically a shortcut you can use to repeat the previous command.

So, typical scenario is that you try a command, and it kicks back a message saying you have to be an admin to do that. So, you can either type sudo to run that command as super-user/admin, or you can type sudo !! where !! tells the system to use the previous command that was attempted.UfH

There are many other bang-commands. For a list of them and explanations to what they are, check out Linux Bang Commands, see also Bash history and bang commands

7
  • 12
    It's also a very dangerous command if you're not 100% sure what your previous command was. It's almost always safer to just hit the up arrow, then the home key, then type sudo and look at the command line. Nov 18, 2013 at 16:06
  • 3
    @Shadur Usually your previous command was about 1 second ago. The normal use case is the one described in the answer: you forget to sudo something and get an error, so you immediately want to sudo it Nov 18, 2013 at 16:21
  • 1
    @Braiam: CLI is an abbreviation. Community rule violation! Attack! Re-edit!
    – Indian
    Nov 18, 2013 at 17:52
  • 4
    sudo doesn't mean "super-user do", it goes as do a command as if I had done su (su meaning switch user [and not super user]); i.e., you can sudo commands as any user, not only root (-u switch); same goes with su [user] [-c command]
    – ssice
    Nov 18, 2013 at 19:11
  • 1
    @Mitch except su does not stand for such a thing. See linux.die.net/man/1/su , gnu.org/software/coreutils/manual/html_node/su-invocation.html , linfo.org/su.html . It should mean substitute user.
    – ssice
    Nov 18, 2013 at 19:32
38

The bang bang (!!) command is a shortcut to repeat and run the previous command you entered in your terminal. This command is very useful when you forget that you need admin rights to make a certain action, and lets you repeat it with super-user rights just by typing,

sudo !!

!! grabs the last run command.

For example:

apt-get update

The output will be,

E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
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?

After that,if we run sudo !! command,the output will be

Hit http://extras.ubuntu.com saucy/main amd64 Packages
Get:3 http://mirror.sov.uk.goscomb.net saucy-updates Release.gpg [933 B]
Hit http://ppa.launchpad.net saucy Release                                  
Hit http://extras.ubuntu.com saucy/main i386 Packages 
Hit http://mirror.sov.uk.goscomb.net saucy Release                             
99% [Waiting for headers] [Waiting for headers] [Waiting for headers]

Which means !! part grabs the previous run command apt-get update and the preceeding sudo part makes the command to run with superuser rights.

And how the sudo !! runs the previous command with superuser privileges means,normally all the commands we entered on the terminal are stored in the command history.Run the history command on the terminal,it shows all the commands you entered.The !! part in the sudo !! grabs the last command stored in the command history and the whole sudo !! runs the last command with admin privileges.

Some other bang commands are explained in this blog post.

4
  • 3
    I'm surprised to see it mentioned 'bang bang' again. I thought once was enough.
    – kiri
    Nov 18, 2013 at 11:08
  • 1
    +1 for more examples of bang commands.
    – WernerCD
    Nov 18, 2013 at 15:45
  • 1
    @Rmano At least in Bash, and I expect all Bourne-style shells with history support, mkdir LongDirectoryName and cd !$ must be issued separately to work, unless LongDirectoryName happens to have been the last word of the preceding command. For shell history purposes, mkdir LongDirectoryName; cd !$ is one command. History interaction with !!, !^, and !$ use the last command the shell actually fully processed before seeing !. Here's an example. If you run history you'll see that commands on a line joined by ; are remembered as one command. Aug 28, 2017 at 20:38
  • @EliahKagan you're right! Deleted wrong info...
    – Rmano
    Aug 29, 2017 at 7:28
13

There are two parts to the answer: !! and sudo

!! is part of the functionality of the shell (in the case of Ubuntu this is probably bash, but other shells like zsh or csh support this, too) called "history expansion". It behaves in a similar way as other expansions in that the shell expands the 'placeholder' to a set of words. While foo* would be expanded to a list of all files starting with 'foo', !! gets expanded to the contents of the previous command line.

$ echo foobar
foobar
$ echo !!
echo foobar
$ !!
echo foobar

Like other expansions, this is done entirely by the shell, so if you type !! after some other command, this command is not aware that there was a !!, but will only see the previous command line. (Unlike other expansions, history expansion happens before a command is saved in the history, that is instead of !! the replaced command line will be saved to the history.)


The sudo command allows executing commands as another user, provided the permissions ar granted by the security policy (default is configured in /etc/sudoers).

By default the root password remains unset in Ubuntu. In order to perform system administration tasks the user created during installation is granted sudo rights. This user can now execute any command on the shell as root, just by prepending sudo. Some GUI programs use the sudo mechanism, too, for example package management.

The reason why sudo can execute other commands as root (or another user) is that the sudo binary (/usr/bin/sudo) has the setuid bit set in its permission and belongs to root. Any (binary) executable with set setuid bit is run with the permissions of its owner. This means that sudo runs effectively with root permissions no matter which user actually called it. Only he internal security policies of sudo manage which user is allowed what and prevent arbitrary users to do arbitrary things.


So, in the case of sudo !! this means

$ mount /dev/sdb1 /mnt
mount: only root can do that
$ sudo !!

is basically identical to

$ mount /dev/sdb1 /mnt
mount: only root can do that
$ sudo mount /dev/sdb1 /mnt

just less typing. In both cases sudo just sees mount /dev/sdb1 /mnt and runs it with root permissions.

5

!! is the syntactically simplest and probably most common expression for history expansion.

As you may have noticed, after substituting the last command executed for !!, bash does two things (in its default configuration):

  1. The full command with the substituted text is shown to you.

    For example, if your command was lshw -c video and you run sudo !! next, then the command after history expansion is sudo lshw -c video.

  2. That command is run.

Normally these two steps occur without interruption and with no opportunity for user interaction, because shopt histverify is unset by default (shopt -u histverify).

However, if you enable shopt histverify (shopt -s histverify) then history expansion works differently:

  1. You get a new primary prompt, with the expanded text automatically entered on it. It is as if you had typed that text yourself, with the cursor at the end, but have not yet run the command.
  2. You, the user, must then press Enter to run the command. Or you may edit the command, cancel it (Ctrl+C), etc. Note that this is not a special prompt, but a regular primary prompt. It really is as though you typed the text yourself.

(Note that the histverify shell option takes effect only if the readline library is being used--but when you use bash interactively on an Ubuntu or other GNU/Linux system, this is essentially always the case.)

Whether or not the histverify shell option is enabled, history expansion is dissimilar to many other shell expansions. Other shell expansions do not show you the expanded command before it is run. Unlike other expansions, which are intended to be used both interactively and noninteractively (for example, in a shell script), history expansion is almost always used interactively.

3

! is used in Linux for history related shortcuts. So, !! will simply run the previous command you executed.

It's very helpful in cases when you either forget to put sudo before a command that requires it or you get Permission Denied or something like that just do

sudo !!
and you're done.

0

!! will repeat and run the previous command , and with sudo it will give the command the root privilege. ( It is not in man page ?!! I can not see it there.)

1
  • For anyone looking for the man page: man --pager='less -p "Event Designators"' bash
    – Sebi
    Feb 9, 2017 at 9:45

You must log in to answer this question.

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