ignore duplicates when browsing bash history

Assum I have the following command history:

  • A
  • B
  • B

When pressing the up arrow on most linux terminals this would cycle like:

  • B
  • A

On osx terminal however it got me:

  • B
  • B
  • A

How can I regain the former behaviour?


Adding

export HISTCONTROL=ignoreboth:erasedups

to ~/.bash_profile worked for me.


From man bash:

HISTCONTROL
A colon-separated list of values controlling how commands are saved on the history list. If
the list of values includes ignorespace, lines which begin with a space character are not
saved in the history list. A value of ignoredups causes lines matching the previous history
entry to not be saved. A value of ignoreboth is shorthand for ignorespace and ignoredups. 
A value of erasedups causes all previous lines matching the current line to be removed from
the history list before that line is saved. Any value not in the above list is ignored.

To mimic the behavior you are used to, run echo $HISTCONTROL on your Linux system and then set the same value on your Mac.