Using Bash History More Efficiently: HISTCONTROL

 in

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

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

you have bash 3

Yahym's picture

you can use:

export HISTCONTROL=erasedups

and you'll never have a duplicate entry in the history.

No need to log out and in

Will Warren's picture

you can just do

# source ~/.bashrc

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

Source Shortcut

ervt's picture

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

Or even better

stevenworr's picture

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

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState