54

I have seen someone typing

apt-get install !!

He did that a few times and every time, a different program was installed. It seems like it automatically figured out what is needed next.

However, when I tried it on my Ubuntu 14.04.1 LTS virtual machine, it just showed some usage instructions. Googling for apt-get install !! doesn't seem to work very well. Many search results do not contain the double exclamation mark.

What does apt-get install !! do and what could be missing on my machine that it did not work?

74

!! is a shortcut in bash that means last command executed.

Try this:

 echo hello
 !!

In this context, the !! will be expanded to another echo hello.

I imagine what happened was they tried to run a command and it failed and complained it wasn't installed. So then they typed sudo apt-get install !! because the package name was coincidentally the same as the name of the command they tried to run. The resulting command would attempt to install a package by the same name as the command they had just attempted to execute.

  • 10
    Yes, that's probably it. If I run lynx, see that it's not installed and then run apt-get install !! it installs lynx. It didn't work for me, because I tried apt-get install !! as the only command. – Thomas Weller May 24 '15 at 19:19
  • 4
    Another common use is when something should have been run as sudo, such as apt-get installing something. – MBraedley May 25 '15 at 19:25
  • 6
    @A.L: you first type apt-get install some-program, and when you realise you don't have privileges, you just sudo !!, which expands to sudo apt-get install some-program. – mgarciaisaia May 26 '15 at 14:32
  • 3
    !! is an alias for !-1, meaning the last item in bash history. !-2 is the penultimate and so on. – Tim May 27 '15 at 11:34
41

!! has nothing to do with apt-get. It is a shell keyword that will be expanded to the previous command. This expansion is done by shell before the current command is interpreted.

From man bash:

!! Refer to the previous command. This is a synonym for `!-1'.

For example, if you run a command:

echo "foo"

Then if you run:

sudo apt-get install !!

it will be expanded to :

sudo apt-get install echo foo

As you can see it is expanded into two packages namely echo and the other is foo. There is no standalone package named foo and echo. Apart from the shell builtin echo there is /bin/echo executable that is a part of the GNU-coreutils package. So, you will get errors saying that no such packages are available.

On the other hand the other person might be doing something like this:

$ cowsay
The program 'cowsay' is currently not installed. You can install it by typing:
sudo apt-get install cowsay

$ sudo apt-get install !!
sudo apt-get install cowsay
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  filters
The following NEW packages will be installed:
  cowsay
0 upgraded, 1 newly installed, 0 to remove and 119 not upgraded.
Need to get 18.5 kB of archives.

As you can see cowsay is being installed.

I would suggest you to not doing this (unless you are sure), being explicit does not harm.

  • 2
    Great explanation for the !!. I knew what it meant, but I like the way you described it. – Terrance May 24 '15 at 20:25
  • 1
    +1 for the warning not to try this at home. – Shadur May 28 '15 at 5:00
16

!! : Refer to the previous command. This is a synonym for !-1 (The source).

For instance, if i execute:

./test

then remember that I need to be Root, then I could simply type:

sudo !!

it's the same as typing:

sudo ./test

It's useful when your last command contain a lot of characters.

  • 1
    Brilliant! I never thought about the sudo !! command; what I have been doing for years is press the up key once, then move the cursor all the way to the left and add sudo. – IQAndreas May 25 '15 at 2:40
  • 1
    @IQAndreas Your way gives you the opportunity to double-check the command, which is not a bad thing when running sudo. – Matt Nordhoff May 25 '15 at 6:16
  • 1
    I always read "sudo !!" as "i meant sudo for f*** sake" – Viktor Mellgren May 25 '15 at 11:32
  • 1
    @Vixen: In that case, thefuck could also be something for you – Thomas Weller May 25 '15 at 16:56
  • @IQAndreas moving the cursor all the way to the left seems too boring. Why not just Ctrl+A or at least Home? – Ruslan May 26 '15 at 7:26
2

The question has already been answered, so I won't be repetitive. However, I would like to point out that there are numerous such shortcuts in various shells that help repeat/find/edit commands. Some of the ones that I use are:

  1. !$ followed by ENTER will type the last word of the last command. For example,

    ls -l /let/me/in
    cd !$
    

This will land you in /let/me/in

  1. Ctrl-R and then type a few words from a previous command. For example, if you typed vi /home/mycoolproject.sh and followed it up with some random commands like ls, cd, pwd, etc. then just typing Ctrl-R followed by cool will pull up that vi command.

Just search for bash (or csh or whatever) keyboard shortcuts and it will be the best thing you did today!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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