Using History Feature On Bash Shell On Ubuntu 16.04 LTS Server
Table of Contents
The Bash (Bourne again shell) is a popular shell used on most 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 that 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!
Listing History
When history command used with no arguments passed, it will display recent commands used with corresponding line numbers.
$ history
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 the exclamation marks 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:
$ !7
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.
$ !! Or $ !-1
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”.
Clearing history
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.
Useful aliases
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
Bash Shell Variable | Description |
HISTFILE | Location of the physical file where history stores the commands. The bash shell defaults to “~/.bash_history”. Setting “/dev/null”, will disable history recording. |
HISTFILESIZE | Refers to a number of commands to be stored in the history file. |
HISTSIZE | 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. |
HISTIGNORE |
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. HISTIGNORE=”ls*:top:clear” |
HISTTIMEFORMAT |
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 |
HISTCONTROL helps in ignoring duplicates. HISTCONTROL can be set with values: erasedups, ignorespace, ignoredups, or ignoreboth. (colon separated)Following are some examples:
HISTCONTROL=ignoredups:erasedups HISTCONTROL=ignoreboth 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: |
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
HISTSIZE=2000# control the size of the commands to be stored in the file
HISTFILESIZE=10000
# location of the physical file where commands are written
HISTFILE=/var/users/rjujare/.bash_history
#Format for recording and displaying purpose: inclusion of timestamp
HISTTIMEFORMAT=’%F %T ‘
# ignore common commands like ls
HISTIGNORE=”ls:id:uname:clear:top”
# ignore duplicate commands and commands starting with space
HISTCONTROL=ignoreboth
# 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
Conclusion
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!
LinuxAndUbuntu Newsletter
Join the newsletter to receive the latest updates in your inbox.