1

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 
  • 1
    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
1

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.