17.11. Command History
The history
command lists the recent history
of commands that have been run by the shell.
Sometimes it can be useful for a user to see commands
that have been run in the past. For example, to remember
the specific command line arguments they used to a program.
It also allows a user to re-execute commands with a special
shorthand notation to re-run a specific command from the shell’s
history.
Here is an example run of the history
command:
$ history 35 8:17 ls 36 8:17 cd classes/CS31 37 8:17 echo "hi Sam" 38 8:17 ./a.out infile.txt outfile.txt & 39 8:18 ./a.out & 40 8:18 ps 41 8:18 pkill a.out 42 8:18 ps 43 8:18 history
This example shows a series of a few commands run in the shell’s recent
history. With each command in the history is a number indicating its
position in the history and the time the command was started. The
most recently run command was the 43nd command, history
. To re-run
a command from the history, you can use !
followed by the number of
the command, and !!
runs the most recent previous command again.
This is particularly useful for easily re-running commands that have a long
list of command line arguments. For example, to re-run command 37
from
the history (echo "hi Sam"
), a user just needs to type !37
at
the shell prompt:
$ !37 hi Sam $ history 35 8:17 ls 36 8:17 cd classes/CS31 37 8:17 echo "hi Sam" 38 8:17 ./a.out infile.txt outfile.txt & 39 8:18 ./a.out & 40 8:18 ps 41 8:18 pkill a.out 42 8:18 ps 43 8:18 history 44 8:20 echo "hi Sam" 45 8:20 history
Note that !37
does not show up in the history, but that command number 44
from the history lists the same command line as command line 37
, the one
that was re-run by entering 37!
.
17.11.1. References
For more information see:
-
The history man page:
man history
-
most used Unix commands from cheat-sheets.org
-
Bash Reference Manual from gnu.org.