Using Bash History More Efficiently: HISTCONTROL

July 15th, 2009 by Cheng Renquan in

Your rating: None Average: 4.2 (12 votes)

Using the HISTCONTROL variable you can control how bash stores your command history. You can tell it to ignore duplicate commands and/or to ignore commands that have leading whitespace.

When working at the command line we often end up executing some commands multiple times. The default history size is 500, too many duplicates of the same commands can fill up your history and leave you with a less then useful history. You can of course increase the size of your history list using HISTSIZE or HISTFILESIZE.

Another alternative is to tell bash not to store duplicates. This is done with the HISTCONTROL variable. HISTCONTROL controls how bash stores command history. Currently there are two possible flags: ignorespace and ignoredups. The ignorespace flag tells bash to ignore commands that start with spaces. The other flag, ignoredups, tells bash to ignore duplicates. You can concatenate and separate the values with a colon, ignorespace:ignoredups, if you wish to specify both values, or you can just specify ignoreboth.

You can set the flags in your ~/.bashrc file or in the global /etc/bash.bashrc file. The following command would append it to your ~/.bashrc file:

  $ echo "HISTCONTROL=ignoreboth" >>~/.bashrc

Now logout and login, type some commands, try the same command numerous times. Now check your history using the up arrow or do:

  $ history|more

You shouldn't see any duplicates in your history.

The history control option, ignorespace, is useful for executing commands that you don't want to record in your command history.

__________________________

Cheng Renquan, Shenzhen, China


Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Will Warren's picture

No need to log out and in

On July 16th, 2009 Will Warren (not verified) says:

you can just do

# source ~/.bashrc

instead of logging out and in, it just saves a little time :D

ervt's picture

Source Shortcut

On July 24th, 2009 ervt (not verified) says:

Or just simply do: . ~/.bashrc
The early Unix folks must have run this command a lot.

stevenworr's picture

Or even better

On July 16th, 2009 stevenworr says:

Add this to your ~/.bash_profile

export HISTCONTROL=ignoreboth

Then there's not need to set a local copy in each subshell. Each subshell would just inherit it.

__________________________

Steven W. Orr

Post new comment

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Tech Tip Videos

From the Magazine

December 2009, #188

If last month's Infrastrucuture issue was too "big" for you then try on this month's Embedded issue. Find out how to use Player for programming mobile robots, build a humidity controller for your root cellar, find out how to reduce the boot time of your embedded system, and if you're new to embedded systems find out the basics that go into one. You can also read about the Beagle Board, the Mesh Potato and a spate of other interestingly named items. And along with our regular columns don't miss our new monthly column: Economy Size Geek.







Read this issue