Shells provide an interesting feature that allows you to find out the history of all commands you have executed previously in a shell. It often happens that we forget what command was typed on the previous day to perform a task. We may or may not be able to recall the exact syntax, but it is very convenient that we can refer to the history saved by the shell.
There are shell variables that can be altered to change what and how much history a user can see. These shell variables are mentioned in the following table:
Name |
Value |
---|---|
HISTFILE |
Name of file in which by default history will be saved |
HISTFILESIZE |
Number of commands to be kept in history file |
HISTSIZE |
Number of history to be stored in memory for current session |
HISTCONTROL |
A colon-separated list of values controlling how commands are saved on the history list |
The value of the HISTCONTROL
shell variable can be:
Value |
Description |
---|---|
ignorespace |
Lines which starts with a blank space, doesn't save in history list |
ignoredups |
Don't save lines which matches in previous saved history list |
ignoreboth |
Applies both ignorespace and ignoredups |
erasedups |
Remove all previous lines from history matching current line before saving it in history file |
Let's see what values these shell variables may contain:
$ echo $HISTFILE /home/foo/.bash_history $ echo $HISTFILESIZE 1000 $ echo $HISTSIZE 1000 $ echo $HISTCONTROL ignoredups
From the value obtained, we can see that the default history is saved into the .bash_history
file of a user's home
directory, with the maximum history command lines saved as 1000. Also, any duplicate history that is already present in the previous history line isn't saved.
Shells provide the history
builtin command so that a user will know the history of commands executed up to now.
Running the history without any options, prints all the previously typed commands on stdout
. The sequence of commands are provided oldest to latest as we go from top to bottom of the output:
$ history # Prints all commands typed previously on stdout $ history | tail -n10 # Prints last 10 commands executed
The following table explains the options available with the history
shell built - in command:
Option |
Description |
---|---|
-a |
Append the new history lines into history immediately |
-c |
Clears history from current list |
-d offset |
Deletes history from offset specified |
-r |
Append the content of saved history to current list |
-w |
Write the current history list to the history file after overwriting existing saved history contents |
To see the last five commands executed, we can also perform the following commands:
$ history 5 769 cd /tmp/ 770 vi hello 771 cd ~ 772 vi .bashrc 773 history 5
We will find that all the commands executed match a given string from the history file. For example, search for commands having the set
string in them:
$ history | grep set 555 man setenv 600 set | grep ENV_VAR2 601 unset ENV_VAR2 602 set | grep ENV_VAR2 603 unset -u ENV_VAR2 604 set -u ENV_VAR2 605 set | grep ENV_VAR2 737 set |grep HIST 778 history | grep set
To clear all the history of commands saved and to append the history available in the current list, we can do the following (don't run the following commands if you don't want to loose the saved command history):
$ history -c # Clears history from current list $ history -w # Overwrite history file and writes current list which is empty
By default, shell has some values set for managing the history. In the previous section, we saw that a maximum of 1000 lines of history will be stored in the history file. If a user spends most of his time working with a shell, he may have used 1000 or above commands in one or two days. In such a case, he will not be able to look at the history if he has typed a command ten days ago. Depending upon the individual use-case, a user can modify the number of lines to be stored in the history file.
Executing the following command will set the maximum number of lines the history file may have to 100000
:
$ HISTFILESIZE=100000
Similarly, we can change where the history file should be saved. We saw that, by default, it is saved in the .bash_history
file in the home
directory. We can modify the HISTFILE
shell variable and set it to whatever location we want our command history to be saved to:
$ HISTFILE=~/customized_history_path
Now the executed command history will be saved in the customized_history_path
file in the home directory instead of the ~/.bash_history
file.
To make these changes reflect to all the shells being launched by a user and for all sessions, add these modifications to the ~/.bashrc
file.
Depending upon a user's history size setting, the number of commands available in the history may be large. If a user wants to look for a specific command, he or she will have to look through the entire history, which can sometimes be troublesome. Shells provide some shortcuts to help us find a specific command previously executed. Knowledge of these shortcuts can save time in finding previously executed commands in the history.
While working in a shell, the [Ctrl + r] shortcut allows you to search for a command in the history. Start typing a command after pressing [Ctrl + r]; the shell shows a complete command that matches the substring of the command typed. To move forward to the next match, type [Ctrl + r] on the keyboard again and so on:
$ [ctrl + r] (reverse-i-search)'his': man history
We can see that typing his
, suggested from history man history
that we previously typed.
The up and down arrow keys available on the keyboard can be used to go back and forward in the history of commands previously executed by the user. For example, to get the previous command, press the up arrow key once. To go back even further, press the up arrow key again and so on. Further, to go forward in the history use the down arrow key.
The shortcut !!
can be used to reexecute the last command executed in the shell:
$ ls /home/ lost+found foo $ !! ls /home/ lost+found foo
3.145.166.241