3

How "sudo !!" works in bash:

$ touch /bin/1
touch: cannot touch `/bin/1': Permission denied
$ sudo !!
sudo touch /bin/1
1
  • Are you asking how sudo changes things, or hot the "!!" is expanded, or both? ooshro gives a very nice explanation for the !!-expansion, below.
    – MadHatter
    Feb 19, 2011 at 8:01

2 Answers 2

12

bash support a history expansion feature. History expansions are implemented by the '!'. '!!' refer to the previous command.

History expansion is performed immediately after a command line is read, before execve() system call.

$ touch /bin/1
touch: невозможно выполнить touch для «/bin/1»: Отказано в доступе
$ sudo strace !!                
sudo strace touch /bin/1
execve("/usr/bin/touch", ["touch", "/bin/1"], [/* 17 vars */]) = 0

bash expansion "sudo !!" to "sudo touch /bin/1" and then run command.

1

sudo !! is just telling the terminal to run the previous command, but this time under sudo. In this case it is the same as just typeing sudo touch /bin/1 the first time.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .