HISTCONTROL Command in Linux with Examples
Last Updated :
11 Oct, 2024
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 duplicate commands or commands with leading white space or both.
Syntax
HISTCONTROL=value
Replace the value with one of the options (ignoredups, ignorespace, ignoreboth, or erasedups).
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 waste 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.
HISTCONTROL command Options
1. ignoredups: Ignore Consecutive Duplicate Commands
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.

2. ignorespace: Ignore Commands with Leading Whitespace
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.

3. ignoreboth: Combine ignorespace and ignoredups
The ignoreboth option is a convenient way to apply both ignoredups and ignorespace simultaneously. It will prevent consecutive duplicate commands and commands with leading whitespaces from being stored in the history.
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.

4. erasedups: Eliminate All Previous Duplicates
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:

Conclusion
The HISTCONTROL variable in Linux provides powerful options for managing and optimizing the Bash command history. By setting values such as ignoredups, ignorespace, ignoreboth, or erasedups, you can customize how commands are saved in the history list to avoid redundancy and protect sensitive commands.
Additionally, using related environment variables like HISTSIZE, HISTFILESIZE, and HISTIGNORE allows for even finer control over history management, making your command-line experience more efficient and user-friendly.
Similar Reads
htop command in Linux with examples htop command in Linux system is a command line utility that allows the user to interactively monitor the systemâs vital resources or serverâs processes in real-time. htop is a newer program compared to top command, and it offers many improvements over top command. htop supports mouse operation, uses
4 min read
help Command in Linux with examples If youâre new to the Linux operating system and struggling with command-line utilities, the help command is one of the first tools you should learn. As its name suggests, the 'help' command provides detailed information about built-in shell commands, making it an essential resource for beginners and
5 min read
getent command in Linux with examples The 'getent' command in Linux is a powerful tool that allows users to access entries from various important text files or databases managed by the Name Service Switch (NSS) library. This command is widely used for retrieving user and group information, among other data, stored in databases such as '
5 min read
fc Command in Linux with Examples As we all know that LINUX is command friendly and while working on LINUX, you may deal with very long commands that may include long paths or really difficult syntax, and imagine what if working with such commands you do a minor mistake which will require re-writing of the entire command synopsis an
3 min read
getopts Command in Linux with Examples The getopts is a very convenient bash script utility command. It helps us to conveniently and gracefully handle the passing of flags(short version), and arguments in a clean, standardized manner. Instead of having the developer manually handle the flags passed to a script, this provides a handy way
6 min read
function command in Linux with examples The function is a command in Linux that is used to create functions or methods. It is used to perform a specific task or a set of instructions. It allows users to create shortcuts for lengthy tasks making the command-line experience more efficient and convenient. The function can be created in the u
2 min read