The Bash shell maintains a history of the commands that you type. Previous commands can be easily accessed in this history in several ways.
The first and easiest way to recall a previous command is to use the up arrow key. Each press of the up arrow key goes backwards one command through history. If you accidentally go back too far, then the down arrow key will go forwards through the history of commands.
When you find the command that you want to execute, you can use the left arrow keys and right arrow keys to position the cursor for editing. Other useful keys for editing include the Home, End,Backspace and Delete keys.
Another way to use your command history is to execute the history
command to be able to view a numbered history list. The number listed to the left of the command can be used to execute the command again. The history
command also has a number of options and arguments which can manipulate which commands will be stored or displayed.
Execute a few commands and then execute the history command:
date
clear
echo Hi
history
The date
command will print the time and date on the system. The clear
command clears the screen.
Your output should be similar to the following:
To view a limited number of commands, the history
command can take a number as a parameter to display exactly that many recent entries. Type the following command to display the last five commands from your history:
history 5
Your output should be similar to the following:
To execute a command again, type the exclamation point and the history list number. For example, to execute the 94th command in your history list, you would execute the following:
!94
Next, experiment with accessing your history using the up arrow keys and down arrow keys. Keep pressing the up arrow key until you find a command you want to execute. If necessary use other keys to edit the command and then press Enter to execute the command.