Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

What does sudo !! do and how?

share|improve this question
7  
Duplicate of, except from the other side: Run same command again but as sudo –  Michael Kjörling Nov 18 '13 at 11:59
5  
@WarrenHill he doesn't want the "how" but the "what" and "how". He ask more about the !! part of the command. –  Braiam Nov 18 '13 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 '13 at 18:14
    
@MichaelKjörling you meant related right? –  Braiam Nov 18 '13 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. –  Michael Kjörling Nov 18 '13 at 18:50

6 Answers 6

up vote 63 down vote accepted

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

share|improve this answer
14  
Note: ! commands are usually not available in bash scripts (only in interactive sessions). They can be disabled with set +o histexpand. –  Benoit Nov 18 '13 at 12:26

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

share|improve this answer
9  
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. –  Shadur Nov 18 '13 at 16:06
2  
@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 –  Michael Mrozek Nov 18 '13 at 16:21
1  
@Braiam: CLI is an abbreviation. Community rule violation! Attack! Re-edit! –  Indian Nov 18 '13 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 '13 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 '13 at 19:32

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.

share|improve this answer
3  
I'm surprised to see it mentioned 'bang bang' again. I thought once was enough. –  minerz029 Nov 18 '13 at 11:08
1  
+1 for more examples of bang commands. –  WernerCD Nov 18 '13 at 15:45
4  
Another very useful one is !$ (last argument). In mkdir LongDirectoryName; cd !$ –  Rmano Nov 18 '13 at 16:03

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.

share|improve this answer

!! 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 am not seeing it there.)

share|improve this answer

! 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 before a command that requires it or you get Permission Denied or something like that just do

sudo !!
and you're done.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.