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:
Listing last ‘n’ commands executed
By using “history <n>”, last ‘n’ commands executed can be retrieved as follows:
$ history 7
Run a specific command from history
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:
Run a previous command again
In many situations, we need to run a command again and again. Below commands will do that job by executing the previous command used.
Searching the commands
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”:
$ history | grep free
To view only last 4 commands:
$ history | tail -4
The more command allows displaying output in the terminal one page at a time:
$ history | more
Run a previous command that starts with a specific word
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:
$ history -c
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.
$ alias 'h=history'
The below alias “ht” displays ten most recent commands -
$ alias 'ht=history | tail'
The below alias “hg” allows displaying all the commands which match the given string -
$ alias 'hg=history | grep'
Add these aliases in “~/.bashrc” file to make them persistent each time the system boots.
History configuration settings
Advanced configuration settings
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
$ PROMPT_COMMAND="history -a; history -c; history -r"
“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:
# control the size of the commands to be recorded in the memory
# control the size of the commands to be stored in the file
# location of the physical file where commands are written
#Format for recording and displaying purpose: inclusion of timestamp
HISTTIMEFORMAT=’%F %T ‘
# ignore common commands like ls
# ignore duplicate commands and commands starting with space
# append, don’t overwrite the history
shopt -s histappend
PROMPT_COMMAND="history -a; history -c; history -r"
How to save the configuration?
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:
$ source ~/.bashrc
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!