Sometimes I’m working on a server that requires some commands to be run via sudo, of course I always seem to forget to add sudo and then get hit with inevitable error message… Fortunately it’s easy to rerun the last command as sudo with:

$ sudo !!

Example:

$ ./sudo_only_script.sh
  zsh: permission denied: ./sudo_only_script.sh
$ sudo !!
  Doing sudo only stuff...

There are lots of other options for rerunning commands here: www.gnu.org/software/bash/manual/bashref.html#Event-Designators

Here are a few more examples:

$ !n
# Refer to command line n.
$ !-n
# Refer to the command n lines back.
$ !string
# Refer to the most recent command preceding the current position
# in the history list starting with string.
$ ^string1^string2^
# Quick Substitution. Repeat the last command, replacing string1 with string2.
# Equivalent to !!:s/string1/string2/.