I am a user of Ubuntu with not much experience and I have been using
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 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
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,
!! grabs the last run command.
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]
!! 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:
!! 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.)
sudo command allows executing commands as another user, provided the permissions ar granted by the security policy (default is configured in
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.
!! 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):
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.
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:
(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.
! 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.
!! 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.)