What does `!!` mean in `sudo !!`?

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

What does sudo !! do and how?


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


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


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.


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.