How to make bash (Terminal.app) remember history of previous sessions?
Solution 1:
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
Solution 2:
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.
Solution 3:
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
Solution 4:
This answer from this Reddit thread saved me:
It's probably RVM preventing the exit "hook" for bash_sessions to run. If you comment out the following line in your .bash_profile, it should work.
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"