Related Articles

Related Articles

HISTCONTROL Command in Linux with Examples
  • Last Updated : 27 Oct, 2020

In Linux, the history command can be used to show the list of commands which have been recently executed. HISTCONTROL variable is a colon-separated list of values controlling how commands are saved in the history list. HISTCONTROL variable allows us to store the bash history more efficiently. It can be used to ignore the duplicate commands or commands with leading white space or both.

Uses of HISTCONTROL command:

  • Several commands are executed multiple times while working at the command line. The default History size is 500. So storage of multiple commands will be a wastage of memory. Though the default history size can be changed using HISTFILESIZE, it is favorable to eliminate the duplicates. This can be achieved using HISTCONTROL.
  • When we don’t want certain commands to appear in History, it can be accomplished using HISTCONTROL variable. We can instruct history to ignore the command by giving a white space before the command.

Working with HISTCONTROL command

1. ignoredups: It causes lines matching the previous history entry to not be included. It does not add a command to history if it’s the same as the immediate previous command. It doesn’t look further back in the history list.

HISTCONTROL=ignoredups

The history after this command is executed will not store a command which is the same as the most recent command used.  Consider that the below commands are added in sequence after using ignoredups:

pwd
whoami
date
pwd
pwd
whoami

When two pwd commands are entered consecutively, the second command is ignored and not added to the history. But if the immediate previous command is not pwd, then it will not be ignored.



histcontrol

2. ignorespace: causes lines which begin with a white space character to not be included in the history list. If we don’t want a command to be included in history we can use white space characters before the command to avoid its inclusion in the history list.

HISTCONTROL=ignorespace

Consider that the below commands are added in sequence after using ignorespace and we don’t want to include the date command, so a white space character can be added before it:

pwd
whoami
date
ls -l | wc -l

As there is a white space before the date command, it will not appear in the history list.

histcontrol ignorespace

3. ignoreboth: is used when we want to use both ignorespace and ignoredups.

HISTCONTROL=ignoreboth

It is the same as:

HISTCONTROL=ignorespace:ignoredups

The history will not include commands with leading white space characters and duplicates. Consider that the below commands are added in sequence after using ignoreboth:

whoami
pwd
pwd
pwd

Only one pwd command is added to the history list as one pwd command has a white space character before it while the other is the same as the most recent command included in the history list.

histcontrol ignoreboth

4. erasedups: It allows all previous lines matching the current line to be eliminated from the history list before that line is saved. The second and subsequent lines of a multi-line compound command are not tested and are added to the history in any case of the value of HISTCONTROL. Its syntax is:

HISTCONTROL=erasedups

The history after this command is used will not store any duplicate element. After a command is executed, it will match it with other recent commands before appending it to the history and if the match is found, then the command is ignored and not stored in the history. Consider that the below commands are added in sequence after using erasedups :

pwd
whoami
date
ls -l | wc -l
pwd
whoami

whoami and pwd commands have been used twice but it won’t append to the history twice after erasedups have been implemented. Output can be seen in the image below:

hsicontrol erasedumps

My Personal Notes arrow_drop_up
Recommended Articles
Page :