Why does this happen?
MacOS and Ubuntu are configured differently out of the box to handle duplicates in bash's command history. These configurations are stored in a number of so-called "dot-files". These take the form of ~/.bash* as well the system wide /etc/profile. All of these files might be customised to your liking and differentiate between interactive shells, login shells, remote shells etc. These files are read in a specific order and serve specific functions.
How to get the same behaviour on macOS?
If you want just this one, single customisation of "ignoring exact duplicates of command lines" you may go with something like Allan's answer, i.e. add a single single line to for example your bash_profile file. There is no "the proper way" but countless options.
In case this is not the only customisation for your bash then this might be not the best option:
A few other notes:
- Anything that should be available to graphical applications OR to sh (or bash invoked as sh) MUST be in ~/.profile
- ~/.bashrc must not output anything
- Anything that should be available only to login shells should go in ~/.profile
- Ensure that ~/.bash_login does not exist.
That means, when things get more complex it is good idea to spread out the customisations into multiple files, each of them specialised and highly ordered in their contents:
exports may reside in their own file for simplified oversight.
Create a file that is read by bash at the root of your user directory, for example called
.exports that contains:
# Omit duplicates and commands that begin with a space from history.
This needs to be "sourced" so that the file is read by bash on interactive startup:
If you have a lot of shell configurations, you may want to split them out into several subfiles and use the source builtin to load them from your .bashrc:
source ~/.exports to it.
Alternatively, to ensure the files actually exist before loading
if [ -f ~/.exports ]; then
. ~/.exports will source
~/.exports in the context of the currently running shell.
This is particularly useful for adding aliases, the separate file makes it easier to re-load them when you make changes.