42

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!

9
  • Hi Nelson, welcome to Super User. Quick check- ere those spaces inserted by you?
    – bertieb
    Aug 4, 2015 at 9:20
  • yes they were, i'll remove them and report back.
    – user477774
    Aug 4, 2015 at 9:20
  • @bertieb, I've removed the spaces and now $HISTFILESIZE is properly echo'd. However, history remains unsaved and $HISTFILE prints out that odd temp history file (which I'm assuming stores history for just that session).
    – user477774
    Aug 4, 2015 at 9:22
  • Are you trying to change $HISTFILE, out of interest? I don't have a .bashrc on OSX and echo $HISTFILE reports the place I would expect- does commenting out the lines in question have any effect?
    – bertieb
    Aug 4, 2015 at 9:42
  • 1
    Always quote pathname variable expansions: HISTFILE="$HOME/.bash_history”. Without the quotes, your version will be invalid if the path to your home directory contains spaces (or possibly other special characters).
    – Chris Page
    Dec 30, 2015 at 12:48

7 Answers 7

32

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

5
  • 3
    But 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
    Dec 30, 2015 at 12:41
  • 1
    @ChrisPage Actually Apple changed some bits of script. Updated answer, thanks.
    – diimdeep
    Dec 30, 2015 at 15:07
  • @diimdeep Where would I append this line? export SHELL_SESSION_HISTORY=0
    – zerohedge
    May 14, 2017 at 0:23
  • @zerohedge .bashrc and .bash_profile unix.stackexchange.com/a/310150/15362
    – diimdeep
    May 14, 2017 at 6:57
  • @diimdeep Thank you. This seems to be working right now. Does it have any ramifications?
    – zerohedge
    May 14, 2017 at 13:52
6

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.

3
  • But 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
    Dec 30, 2015 at 12:44
  • Thanks 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?
    – rabs
    Feb 25, 2016 at 0:53
  • @rabs I'd suggest adding SHELL_SESSION_HISTORY=0 at the top of ~/.bash_profile.
    – Teejay
    Dec 27, 2017 at 16:18
5

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.

3
  • 1
    Nice fix, without having to modify rvm.
    – mlo55
    Aug 8, 2017 at 6:31
  • 1
    Are we talking about the Ruby enVironment Manager (RVM) rvm.io ?? When and why should that become involved?
    – MarkHu
    Jun 3, 2019 at 20:48
  • Suggest: echo 'shell_session_update' >> $HOME/.bash_logout to preserve the possible file content. Jun 2, 2020 at 10:13
3

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"
1
  • This indeed helped!
    – Karsten
    Mar 19, 2017 at 21:06
1

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.

8
  • I've edited .profile so it is the same as my .bashrc, and history still doesn't come up.
    – user477774
    Aug 7, 2015 at 18:18
  • @NelsonLiu What happens when you echo $HISTFILE in different parts of profile? Maybe you source another script that changes the variable? I checked both Terminal and iTerm, bash 3 and 4 both have their default $HOME/.bash_history.
    – theoden8
    Aug 7, 2015 at 18:52
  • how would I echo $HISTFILE in different parts of profile?
    – user477774
    Aug 8, 2015 at 19:12
  • @NelsonLiu, isn't it obvious? your aim is to track when does $HISTFILE change. Thus, simply make two echo $HISTFILE surround your code. What you have to do is to find the piece of code where $HISTFILE changes by moving both echo $HISTFILE lines closer and closer to each other line-by-line until something reveals. That's all.
    – theoden8
    Aug 8, 2015 at 19:16
  • 1
    so 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 ran echo $HISTFILE in the shell, and it outputted /Users/nelsonliu/.bash_sessions/CD275A29-1DF1-4ED8-B8CE-F706B11B812F.historynew.
    – user477774
    Aug 12, 2015 at 18:06
0

I was seeing this problem on High Sierra. Somehow, my own .bash_history had become owned by root and not even having read permissions for other users (when contents of home directory viewed with ls -al)

There was nothing of any consequence in this .bash_history file, so I did a sudo rm .bash_history followed by a touch .bash_history to make a new one.

All seems well now

1
  • For 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 '
    – MarkHu
    Jun 3, 2019 at 21:15
0

TLDR; unset $HISTFILE in the zsh / bash RC file


I had a similar issue with zsh

  • the $HISTFILE was set to ~/.history in ~/.zshrc
  • it was somehow overridden by the OS and ended up in the ~/.zsh_session/XXX directory (so never saved to the local history file)

But since it was set in my RC file, the shell history did load it from the old ~/.history and was never up to date

Removing the "manual" setting of the $HISTFILE solved the issue in my case

You must log in to answer this question.