Code, Computers & Random Junk

Avoid Duplicates in Bash History

Here are just some minor tweaks you can add to your .bashrc (or if you use .bash_profile) to sort out duplicates in your bash history. Also, a tweak to use in OS X how to put all defaults write to a separate file.

“Defaults write”

All our beloved commands we run to tweak our environment &/or a program. It’s a good idea to put them in a separate file to know which one we have used before. So when moving to a new system or performing a clean install, we have them gathered in a separate file.

I keep mine here:

  • ~/ShellScripts/__defaultsLog.txt

“ShellScripts” is a folder I keep all (most) my shellscripts in. But you can of course choose any place you want. So in your .bashrc you can add:

# Screen for defaults write
PROMPT_COMMAND='echo "$(history 1 | grep "defaults")" | sed '/^$/d' >> $HOME/ShellScripts/__defaultsLog.txt'

This will add each command matching “defaults” to our log file.


To get a cleaner bash history it’s great to avoid duplicates. First I have set the size to something quite big.

export HISTFILESIZE=15000
export HISTSIZE=15000

To avoid the duplicates add:

# No duplicates
# ignoreboth (ignoredups + ignorespace)
export HISTCONTROL=ignoreboth:erasedups

Ignoreboth is both ignoredupes and ignorespace. Ignorespace is really useful. When you run a command and start with a blank space it will not be added to the history. Great when running stuff you don’t want in there - like passwords.

Then I use these two:

shopt -s histappend

This will also add the entry you just made immediately to the history, which is great. When working with multiple windows you’ll get access to it right away in the other ones.