Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How "sudo !!" works in bash:

$ touch /bin/1
touch: cannot touch `/bin/1': Permission denied
$ sudo !!
sudo touch /bin/1
share|improve this question
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 '11 at 8:01
up vote 12 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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