Last Updated: February 25, 2016
· rishav

Run the last shell command with sudo - Mac OS X

Haven't you had that moment when you type in a long command and forgot you needed to run it as root, gah!!!

There is a simple shortcut to save us from copy pasting or typing it all over again.

To run the last command 'as is', you can just do

$ !!

If you want to run it as root, with sudo; just do

$ sudo !!

3 Responses
Add your response



over 1 year ago ·

More generically, if you need to re-run the next-to-last command, you can use '!-2', or the Nth previous command with '!-N'. Or, if you review your command history ('history | tail -20'), and see that you need to re-run the 134th command in your history, '!134'.

over 1 year ago ·

@dougal :) Didn't know that. Cheers, Thanks.

over 1 year ago ·
Filed Under