Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Because it seems often the history isn't saved when shutting down and not closing gnome terminal, I'd like to append to bash history before showing the prompt if the line isn't a duplicate. Although I have export HISTCONTROL=ignoreboth in bashrc, which is supposed to imply ignoredups, it doesn't seem to work, as I still get duplicates in bash_history. So how to do this?

To offer further indications, although not solutions: I think $PROMPT_COMMAND needs to have "$(history 1)" (if_not_duplicate) >> ~/.bash_history

And this would remove non-adjacent duplicates, if they are already inserted.

perl -nei '$H{$_}++ or print' ~/.bash_history 
share|improve this question
ignoredups only removes immediate duplicates - That is, if you run the same command more than once without running any other commands in between, it's only registered once. It doesn't check the entire history for duplicates. – l0b0 Apr 30 '12 at 13:46

You need to modify both PROMPT_COMMAND and HISTCONTROL to do this right. The following should work for you:

# The secret sauce is "erasedups," which replaces the default of "ignoredups."
export HISTCONTROL=erasedups:ignorespace

# Write the history to HISTFILE before showing the prompt.
export PROMPT_COMMAND='history -w'

Two caveats to consider with this approach are:

  1. You will lose history (specifically duplicate entries), which may matter if you rely on HISTTIMEFORMAT values for anything.
  2. You will continuously truncate your HISTFILE to HISTFILESIZE.

These aren't necessarily problems, just minor issues you should be aware of.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .