The Bash (Bourne-again shell) is a popular shell used on most the UNIX-like systems. Bash is written by Brian Fox as GNU Project. It incorporates features from Bourne shell, C shell, and Korn shell. Users who spend their time on the command line using bash shell often will like to repeat the commands those were executed in the past. The Bash shell history feature help in remembering recent commands and reuse them later; thus saving time to retype. Let’s get started!
When history command used with no arguments passed, it will display recent commands used with corresponding line numbers.
The output will be similar to the following:
By using “history <n>”, last ‘n’ commands executed can be retrieved as follows:
It is possible to execute a specific command from history. Using exclamation mark followed by the number of the command as listed by history will do the magic. For instance, following command re-run the command having the number 7 (du –sh) as follows:
In many situations, we need to run a command again and again. Below commands will do that job by executing the previous command used.
The output generated by history can be used in several ways. For instance, the history output can be passed to – more, tail, grep in order to retrieve the desired output.
Search the command history for “free”:
To view only last 4 commands:
The more command allows displaying output in the terminal one page at a time:
Using exclamation followed by the specific word of the command will do the magic!. Suppose that user has executed – ”free –h”. Typing ‘!free’ will re-run the command ‘free –h”.
It is possible to clear the history with below command:
This clears all the commands in the bash shell memory for the current user session.
An alias is a short name given to a long and complex command. Simple single character alias to history command.
The below alias “ht” displays ten most recent commands –
The below alias “hg” allows displaying all the commands which match the given string –
Add these aliases in “~/.bashrc” file to make them persistent each time the system boots.
Bash Shell Variable
Location of the physical file where history stores the commands. The bash shell defaults to “~/.bash_history”. Setting “/dev/null”, will disable history recording.
Refers to a number of commands to be stored in the history file.
Refers to a number of commands that are stored in memory in a history list. Setting a value of 0 (zero) will disable history recording.
Sometimes it is required not to store some kind of commands. This variable takes a list of colon separated patterns. For example, the below configuration make history feature not to record the commands: ls, top and clear.
This HISTTIMEFORMAT variable allows displaying history with time stamp.
Having HISTTIMEFORMAT set to ‘%F %T ‘ yield the below results.
This is a very useful command for system administrators.
HISTCONTROL helps in ignoring duplicates. HISTCONTROL can be set with values: erasedups, ignorespace, ignoredups, or ignoreboth. (colon separated)
Following are some examples:
Suppose the history entries contain duplicates as follows (“top” and “ls –latr” are repeated consecutively)
If HISTCONTROL was set to “ignoredups” then the history would have displayed like below:
Bash maintains the list of commands internally in memory and they are written into the configured file (default being .bash_history) on exit. When the user exit from the shell, the last $HISTSIZE lines are copied from the history list to the file set to $HISTFILE. If a user is logged in with many bash sessions, only the last one to exit will have its history saved. In case the command history to be written immediately as and when a command is executed in a user session, following configuration can be set in the “.bashrc” file.
“shopt –s histappend” enable append instead of overwriting the history.
“history -a” is used to append the history commands in memory to ~/.bash_history
“history -c” is used to clear the history commands in memory
“history -r” is used to read history commands from ~/.bash_history to memory
A typical “~/.bashrc” file with history related configuration setting will appear like below:
Open the ~/.bashrc file with your favorite editor to change the settings. When changes are finished, save the file and exit. In order to get the change effect, source the file using the ‘source’ command as follows:
The Bash shell comes with many useful features and one of them is history. These features greatly improve the productivity of administrators and regular users as well. Do not forget to share your own experiences with history!