!!
Repeats the previous command:
$ ls
$ !!
ls
!n
Repeats the command at position n
in the history list:
$ history
1 ls
2 pwd
3 echo "Hello"
$ !2
pwd
!-n
Repeats the command that was executed n
commands ago:
$ ls
$ pwd
$ echo "Hello"
$ !-3
ls
!string
Repeats the most recent command starting with string
:
$ ls -l
$ pwd
$ !ls
ls -l
!?string
Repeats the most recent command containing string
:
$ ls -al
$ echo "Test"
$ !?al
ls -al
^old^new
Repeats the previous command, but substitutes old
with new
:
$ echo "Hello world"
$ ^world^everyone
echo "Hello everyone"
Hello everyone
!$
Refers to the last argument of the previous command:
$ mkdir new
$ cd !$
cd new
!*
Refers to all the arguments of the previous command:
$ touch file1.txt file2.txt file3.txt
$ chmod o+r !*
chmod o+r file1.txt file2.txt file3.txt
!:^
Refers to the first argument of the previous command:
$ cp file1.txt /some/directory/
$ echo !:^
echo file1.txt
file1.txt
!:n-m
Refers to a range of arguments to the previous command:
$ cp file1.txt file2.txt file3.txt /some/directory
$ echo !:1-2
echo file1.txt file2.txt
file1.txt file2.txt
!!:p
Print the last command without executing it:
$ echo "Hello, World!"
$ !!:p
echo "Hello, World!"