33

Very useful when you forget a sudo at the beginning of your command, !! acts like an alias of the previous command. Example :

$ mv /very/long/path/for/a/protected/sensible/file/caution.h .
(...) Permission denined
$ sudo !!
sudo mv /very/long/path/for(...) .
[sudo] password :
  • How do we call that double !! trick ? Researches over internet are difficult because of that token.
  • How does it work ? I suspect a link with the history command.
  • Where it is defined ? Can I define some other myself ?

EDIT : Some interesting event designators

!!:*

It refers to the arguments of the previous command. Use case :

cat /a/file/to/read/with/long/path
nano !!:*

:p

Just print the command without executing it, you have to put it at the end of the event designator.

$ !-5:p
sudo rm /etc/fstab -f

More here.

  • 3
    Read man history – Costas Aug 25 '16 at 15:41
  • 1
    It's a special case of history expansion, in which the shell attempts to expand a word starting with ! to a matching command in the current shell's history list. !! is a special case, equivalent to !-1, where a negative number n following ! refers to the nth previous command. – chepner Aug 25 '16 at 15:48
  • 1
    @Costas, more usefully, read LESS='+/^HISTORY EXPANSION' man bash. – Wildcard Aug 26 '16 at 2:34
34

!! is listed in the bash manual under the heading "Event Designators":

   An event designator is a reference to a command line  entry  in  the
   history list.  Unless the reference is absolute, events are relative
   to the current position in the history list.

   !      Start a history  substitution,  except  when  followed  by  a
          blank,  newline,  carriage  return,  = or ( (when the extglob
          shell option is enabled using the shopt builtin).
   !n     Refer to command line n.
   !-n    Refer to the current command minus n.
   !!     Refer to the previous command.  This is a synonym for  `!-1'.
   !string
          Refer  to the most recent command preceding the current posi-
          tion in the history list starting with string.
   !?string[?]
          Refer to the most recent command preceding the current  posi-
          tion  in  the history list containing string.  The trailing ?
          may be omitted if string is followed immediately  by  a  new-
          line.
   ^string1^string2^
          Quick  substitution.   Repeat the previous command, replacing
          string1       with       string2.        Equivalent        to
          ``!!:s/string1/string2/'' (see Modifiers below).
   !#     The entire command line typed so far.

So !! will be replaced with the previous command.

Note that the shell history will not contain the literal !! but instead the actual command that was executed:

$ ls
[some output]

$ !! .
[same output]

$ history 3
  645  2016-08-25 17:40:55 ls
  646  2016-08-25 17:40:57 ls .
  647  2016-08-25 17:41:00 history 3

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.