Apple - How to make bash (Terminal.app) remember history of previous sessions?

You'll need to tell bash where to keep your history file, and how many lines to keep:

# Set the location of your HISTFILE
echo "export HISTFILE=/Users/<USERNAME>/.bash_history" >> ~/.bash_profile

# Number of lines to keep (1000 in this example)
echo "export HISTFILESIZE=1000" >> ~/.bash_profile

# Set how many commands to keep in the current session history list
echo "export HISTSIZE=80" >> ~/.bash_profile

# Ignore commands that start with a space
echo "export HISTIGNORE=\"&:[ ]*:exit\"" >> ~/.bash_profile

I just discovered I had a similar problem, though my employer only has two Macs, and neither sits on my desk. Unacceptable, but a battle for another time.

Anyway, at home, Mac Mini (migrated from MacBook Pro) did not cooperate even after setting .bashrc and/or .profile. Then I discovered that my ~/.bash_history file was mysteriously owned by root. Had to do this:

sudo -iu root
cd ~(myusername)
chown (myusername) .bash_history

(Yes, I know it can be done in fewer commands. Habit.)

Then I exited Terminal, and started up a new one. Woot! All sorts of history previously lost came rushing back. How very nice.

Likelihood anyone reading this has accidentally put themselves in this predicament: 0.2%. I'm sure I mucked this up when over-zealously "fixing" something else.


I had this problem and what worked for me was create a file called .bash_sessions_disable in my user folder (~/).

Basically OS X create history files in .bash_sessions folder for each session (in my case, one for each Tab). And even properly closing these sessions, OS X don't replay the historical to .bash_history.

Creating the .bash_sessions_disable you disable this behavior.

touch ~/.bash_sessions_disable