Bash History Not Preserved Between Terminal Sessions on Mac
Solution 1
Terminal assigns each terminal session a unique identifier and communicates it via the TERM_SESSION_ID environment variable so that programs running in a terminal can save/restore application-specific state when quitting and restarting Terminal with Resume enabled.
A new folder (~/.bash_sessions/) is used to store HISTFILE's and .session files that are unique to sessions.
During shell startup the session file is executed. Old files are periodically deleted.
The default behavior arranges to save and restore the bash command history independently for each restored terminal session. It also merges commands into the global history for new sessions.
You may disable this behavior and share a single history by setting
export SHELL_SESSION_HISTORY=0
If HISTTIMEFORMAT is defined, per-session history is disabled by default (read more in /private/etc/bashrc_Apple_Terminal)
The save/restore mechanism is disabled if the following file exists:
~/.bash_sessions_disable
Apple already changed some behavior since El Capitan release, so it is better to go read more about this here less /private/etc/bashrc_Apple_Terminal
Solution 2
I noticed something similar after the El Capitan upgrade. Simply adding the file .bash_sessions_disable
file in your home directory disables the new bash sessions and the .bash_history
is back in use.
This Reddit thread has more info and further links.
Solution 3
You can solve RVM problem by updating to latest RVM version or executing this:
echo 'shell_session_update' > $HOME/.bash_logout
See https://github.com/rvm/rvm/issues/3540 for more info.
Solution 4
This answer from the 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"
Solution 5
If it's a Mac, I suppose it's default login shell is bash
and it runs .profile
instead of .bashrc
. So, you were editing the wrong file.
Related videos on Youtube
Admin
Updated on September 18, 2022Comments
-
Admin over 1 year
My bash history mysteriously stopped working, and I have no idea how to fix it. This is what my .bashrc looks like:
HISTSIZE=500 HISTFILESIZE=500 HISTFILE=$HOME/.bash_history
However, when i run
echo $HISTFILE
it prints out/Users/myusername/.bash_sessions/EE8689E5-7DAD-4018-817E-0AF1DE36082A.historynew
.I am the owner of the .bash_history file, so I'm not too sure how I would go about fixing this issue.
Thanks!
-
Admin almost 9 yearsI've edited .profile so it is the same as my .bashrc, and history still doesn't come up.
-
theoden8 almost 9 years@NelsonLiu What happens when you echo
$HISTFILE
in different parts ofprofile
? Maybe you source another script that changes the variable? I checked bothTerminal
andiTerm
,bash
3
and4
both have their default$HOME/.bash_history
. -
Admin almost 9 yearshow would I echo $HISTFILE in different parts of profile?
-
theoden8 almost 9 years@NelsonLiu, isn't it obvious? your aim is to track when does
$HISTFILE
change. Thus, simply make twoecho $HISTFILE
surround your code. What you have to do is to find the piece of code where$HISTFILE
changes by moving bothecho $HISTFILE
lines closer and closer to each other line-by-line until something reveals. That's all. -
Admin almost 9 yearsHi @theoden, sorry for my misunderstanding. Interestingly enough, when I add a
echo $HISTFILE
to the start and end of my.profile
, no output is produced. when I add anecho $HISTFILE
to the start and end of my.bash_profile
, however, the proper $HISTFILE is outputted. (/Users/myuser/.bash_history). What else should I try? Sorry, I'm pretty new to shell. -
theoden8 almost 9 years@NelsonLiu, if it is really like this, you should 1) make
.bash_profile
same as.profile
2) do as I said before. -
Admin almost 9 yearsso I've done as you instructed and made .bash_profile the same as profile. I decided to
echo $HISTFILE
on every line, just to see if there were any differences. However, it merely printed/Users/username/.bash_history
a myriad of times. I then ranecho $HISTFILE
in the shell, and it outputted/Users/nelsonliu/.bash_sessions/CD275A29-1DF1-4ED8-B8CE-F706B11B812F.historynew
. -
theoden8 almost 9 years@NelsonLiu, this only means that something else is launched after
bashrc
. Nothing works and noone knows why. Perhaps some suspicious app changes the histfile or something. You can only add a little hack to yourbash_profile
for sure:sleep 2 && export HISTFILE=~/.bash_history &
. -
Chris Page over 8 yearsBut don’t just start by disabling the save/restore mechanism. If you’re having issues with the shell command history, try to resolve that issue. The
~/.bash_sessions_disable
file is meant as a last resort in case there’s an issue that can’t be resolved specifically. It disables more than just the per-session command histories, and you can disable just the per-session command history. See the comments in/etc/bashrc_Apple_Terminal
for details. -
Chris Page over 8 yearsBut don’t just start by disabling the save/restore mechanism. If you’re having issues with the shell command history, try to resolve that issue. The
~/.bash_sessions_disable
file is meant as a last resort in case there’s an issue that can’t be resolved specifically. It disables more than just the per-session command histories, and you can disable just the per-session command history. See the comments in/etc/bashrc_Apple_Terminal
for details. -
diimdeep over 8 years@ChrisPage Actually Apple changed some bits of script. Updated answer, thanks.
-
rabs about 8 yearsThanks Chris - what else does it disable my history has been working as expected (same as previous osx version and same as linux) for the last couple of months. Don't know why they changed it?
-
Karsten about 7 yearsThis indeed helped!
-
zerohedge about 7 years@diimdeep Where would I append this line?
export SHELL_SESSION_HISTORY=0
-
diimdeep about 7 years
-
zerohedge about 7 years@diimdeep Thank you. This seems to be working right now. Does it have any ramifications?
-
mlo55 almost 7 yearsNice fix, without having to modify rvm.
-
Teejay over 6 years@rabs I'd suggest adding
SHELL_SESSION_HISTORY=0
at the top of~/.bash_profile
. -
MarkHu almost 5 yearsAre we talking about the Ruby enVironment Manager (RVM) rvm.io ?? When and why should that become involved?
-
MarkHu almost 5 yearsFor me, it wasn't the file permissions, it was that I hadn't defined any of the HIST control variables. Apparently one or more of them must be defined. I added this to my
~/.bash_profile
file:export HISTTIMEFORMAT='%F %T '
-
Jari Turkia almost 4 yearsSuggest:
echo 'shell_session_update' >> $HOME/.bash_logout
to preserve the possible file content.