APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed


I happened to come across Linux Tips: take control of your bash_history. This explains that setting "export HISTCONTROL=ignoreboth" causes Bash not to store duplicate commands in history. For example, if you are checking "ps" waiting for some process to end:

ps -e | grep foo

and keep pressing up arrow every few seconds to repeat it, you'll end up with a long list of those in your history if this isn't set. If it is set, you get only the last.

Actually there's much more to this than that little post exlained.

First, there's actually several values HISTCONTROL can take. On newer bash, it can be set to "ignorespace", "ignoredups", "ignoreboth" or "erasedups". Bash 2.05 doesn't have "erasedups".

If you want manual control over history, set "ignorespace". Then remember to use a space to precede any command you don't want to remember in history. Bash ignores leading spaces, so "  ls" is as good as "ls", but with "ignorespace" set, "  ls" won't be saved.

The "ignoredups" setting ignores repeated lines. Typing "ls" twenty times in a row only ends up with one of them in history. However, typing "ls" and then "ps" and then "ls" again will store "ls" and "ps" every time - unless you have bash 3 and set "erasedups". If that's set, no duplicates get entered in bash history at all.

You can also control bash history with HISTIGNORE. For example, if I
"declare -x HISTIGNORE=ls:ps"
, neither "ls" or "ps" will appear in my history. To ignore all two character commands, use "declare -x HISTIGNORE='??'".

I don't like extra commands cluttering up my history file, so I use "ignoreboth". When available, I use "erasedups", but if you are using history as a record of commands rather than just for the convenience of repetition, you don't want that.

Date stamp

One thing I do sometimes want in my Bash history is a date stamp.

If the purpose is just to manually do this as the need strikes (for example to record the start and stop of various projects), you can simply type:

`date` (no ENTER)

and then ESC CTRL-e ENTER, which of course generates an error but does nonetheless stamp the history file. If you don't like the error, do:

echo `date` ESC CTRL-e ENTER


More Articles by

Find me on Google+

Click here to add your comments
- no registration needed!

Sun Jun 8 02:22:30 2008: 4325   anonymous

I am hoping that the following is just for example's sake, but if not, type "man watch" to learn about your new best friend- in this case the simplest version is: watch "ps -e | grep foo"

"if you are checking "ps" waiting for some process to end:
ps -e | grep foo
and keep pressing up arrow every few seconds to repeat it,"

Sun Jun 8 10:39:41 2008: 4326   TonyLawrence

Yes, it was just for example, but keep in mind that bash runs on lots of systems that do NOT have "watch". or anything like it.

Old Unix folk like me would be unlikely to use it anyway. Out of long habit, we'd write

while :
ps -e | grep foo
sleep 2

or whatever. So "watch" will never be our new best friend :-)

Mon Jun 9 01:18:00 2008: 4327   BigDumbDinosaur

Hey! Watch it with the "old UNIX folks" stuff. I resemble that remark!

Thu Apr 7 15:33:29 2011: 9434   Brade


Thanks for the tips--I've seen similar advice from other pages on the web, including the one you linked. The problem is that, on my system, once the bash session ends, the duplicates reappear the next time a session is opened.

The problem was described by a stackoverflow member here: (link)

This is the exact problem I'm having. No matter if I use "ignoredups" or "erasedups" or both, The history updates correctly while my session is open, but once I close Terminal and reopen later, those duplicates are back. Extremely frustrating, to say the least. You wouldn't happen to know the reason for this? (Or can you duplicate the problem on a OS 10.6.7 machine?)

Thu Apr 7 16:44:12 2011: 9435   TonyLawrence


Running OS X 10.6.7 with

I get no dupes.

Wed Nov 27 12:23:35 2013: Website: 12372   anonymous


echo $(date) ENTER

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments

If you want a picture to show with your comment, go get a Gravatar
Kerio Samepage

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Jump to Comments

Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

This post tagged:

       - Linux

       - MacOSX

       - Shell

       - Unix

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!

book graphic unix and linux troubleshooting guide

Buy Kerio from a dealer who knows tech:
I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals