Each command that we execute in the terminal is logged in the history file. Thus, executing identical commands multiple times will cause the history file to become saturated. Eventually, we will have too many duplicate entries in our Bash history file. In recent Ubuntu based systems, the file size is 1000. Once the number of command entries reaches the limit, the oldest entries will be eliminated from the history list. We can increase the file size to unlimited; however, it is advisable to refrain from storing duplicate entries in the history file.

File location

It is located in user's home directory as a hidden file : .bash_history, but we can locate it with :

echo $HISTFILE

HISTCONTROL variable

HISTCONTROL variable can be used to ignore duplicate commands or commands with leading white space or both.

Syntax :

HISTCONTROL=value

HISTCONTROL can have the following values:

  • ignorespace - lines beginning with a space will not be saved in history.
  • ignoredups - lines matching the previous history entry will not be saved. In other words, duplicates are ignored.
  • ignoreboth - It is shorthand for "ignorespace" and "ignoredups" values. If you set these two values to HISTCONTROL variable, the lines beginning with a space and the duplicates will not be saved.
  • erasedups eliminate duplicates across the whole history.

Two ways to use HISTCONTROL :

 echo "export HISTCONTROL=ignoredups" >>~/.bashrc
 source ~/.bashrc

or edit the bashrc file using nano :

 nano ~/.bashrc

and add the following line at the end of the file : HISTCONTROL=ignoreboth it is the same as HISTCONTROL=ignorespace:ignoredups since HISTCONTROL variable is a colon-separated list of values.

 # ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

save the file then :

source ~/.bashrc

source : 1 2 3 or

man bash

Previous Post Next Post