Re-Run the Last Used Command Exactly With or Without Super User Privileges
Want to re-run the last executed command? Or what about re-running the last used command but running it as root? You can do both!
Ever typed a nice fancy string command into the terminal and been frustrated to discover that you need to run it all over again? Or perhaps you discovered that the aforementioned run command must be run again, but this time as a super user? You too? I do it all the time, but if you do this in the future, there’s no problem and no need to re-enter command sequences, because once you learn this nifty trick with the excellent !! commands you can just quickly re-run the prior command strings.
There’s really two parts to this: running the last command again just as it was, and running the last command again but with super user privileges, aka as root.
How to Run the Last Used Command Again Exactly
First, know that typing !! will re-run the last command, exactly as it ran the first time. It’s easy, try this yourself. First, run any command, pick something simple like ‘ls’ if you want. Then before executing another command, you can use the following string:
This re-runs the exact command that was run immediately prior. If the command failed, it will run as failed again. If the command succeeded, it will run again successfully. Make sense?
How to Run the Last Used Command Again As Super User
Now here’s where things get interesting and extra amazing, you can prefix the !! command with sudo, to re-run the last used command but as root with super user privileges. We’ve touched on running last executed with sudo !! before, but it’s so relevant to the aforementioned command that we can’t leave it out.
Just type this command to do it:
This executes the last used command but through sudo, running it as the root user. It’s basically like typing out the entire command again but prefixing it with ‘sudo’, without having to retype the entire lengthy complex string or syntax again! Helpful indeed.
BTW, these tricks work in both Mac OS X and Linux, so whatever command line environment you find yourself in, re-run those commands when you need to.