Let's have this command:
$ head /etc/passwd /etc/crontab /etc/services
# more output
Repeat latest command:
$ !!
head /etc/passwd /etc/crontab /etc/services
Use latest command as parameters for other command:
$ echo !!
echo head /etc/passwd /etc/crontab /etc/services
head /etc/passwd /etc/crontab /etc/services
Use 2nd parameter from previous command in another command. Count starts from 0:
$ vi !:2
vi /etc/crontab
Use first argument from the previous command:
$ vi !^
vi /etc/passwd
Use last argument from the previous command:
$ vi !$
vi /etc/services
Repeat the nth command from your history.$ !10
Repeat the nth command from bottom of your history.$ !-10
Repeat the last command matching a string’s beginning.$ !ma
mail
Repeat the last command matching anywhere in a string.$ !?links
vi ../links.txt
Repeat but substitute a string.$ head /etc/passwd
# head output
$ !!:s^passwd^crontab
head /etc/crontab
So, it will replace passwd with crontab.$ !he:s^passwd^crontab
head /etc/crontab
Print out commands.$ !!:p
head /etc/crontab
$ head /etc/crontab
# command output
$ !h:p
head /etc/crontab