What does "apt-get install !!" do and how to make it work?
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?
!!
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.
!!
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.
!!
: 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.
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:
-
!$
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
-
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 likels
,cd
,pwd
, etc. then just typing Ctrl-R followed bycool
will pull up thatvi
command.
Just search for bash (or csh or whatever) keyboard shortcuts and it will be the best thing you did today!