The shell provides multiple shortcuts via the usage of the exclamation point when trying to execute recently performed commands. Err the Blog’s Bash Cheat Sheet provides the following tips and more.
If you need to run the most recently executed command again
!!
If the last command run requires super user privileges
sudo !!
Run the most recently executed command beginning with foo
!foo
Retrieve the last ‘word’ of the most recently executed command. In the example below, !$ would have the value my_database.
mysqldump -u root -p my_database
!$ # Will contain the value 'my_database'
Similar to the example above, this shortcut contains all ‘words’ except the first in the most recently run command. Using the same mysqldump command above, !* would have the value -u root -p my_database.
mysqldump -u root -p my_database
!* # Will contain the value '-u root -p my_database'
Note that the above commands can have :p appended, printing the commands or ‘words’ that the exclamation point replacement would use instead of running them as part of a new command in shell. The example below would print the most recently executed command beginning with foo
!foo:p