- bash stores a history of commands executed
- history lists all commands
- history N lists the last N commands
$ history 5 14 cd /tmp 15 ls -l 16 cd 17 cp /etc/passwd 18 vi passwd
In addition to the basic command recall with the arrow keys, the bash history mechanism supports a variety of advanced ways of retrieving commands from the history list.
!! Repeats last command !char repeats last command that started with char !num repeats a command by its number in history output
To View past commands with the history numbers, use the history command.
Other slightly more advanced history tricks include:
!?abc repeats last command that contains (as opposed to ?started with?) abc !-n repeats a command entered n commands back
Use ^old^new to repeat the last command with old changed to new, for example:
[student@stationX ~]$ cp filter.c /usr/local/src/project [student@stationX ~]$ ^filter^frontend cp frontend.c /usr/local/src/project
More History Tricks
- Use the up and down keys to scroll through previous commands.
- Type Ctrl-r to search for a command in command history.
- To recall last argument from previous command:
- Esc (the escape key followed by a period)
- Alt (hold down the alt key while pressing the period)
- Can be pressed multiple times
- !$ (only valid for the last command)
Using your history is a great productivity-enhancing tool. Linux users who develop a habit of using their history can streamline and speed their use of the shell. Try playing with the keystrokes listed above.
You can ignore repeated duplicate commands and repeated lines that only differ in prepended sapces by running the command below, or by adding it to your ~/ .bashrc file.
[student@stationX ~] export HISTCONTROL=ignoreboth