Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

2 Answers

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
add comment

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
add comment

Your Answer

 
discard

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.