Bash History Not Preserved Between Terminal Sessions on Mac

33

16

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!

user477774

Posted 2015-08-04T09:18:49.423

Reputation:

Hi Nelson, welcome to Super User. Quick check- ere those spaces inserted by you? – bertieb – 2015-08-04T09:20:20.880

yes they were, i'll remove them and report back. – None – 2015-08-04T09:20:49.827

@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). – None – 2015-08-04T09:22:57.607

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 – 2015-08-04T09:42:14.057

Where are you sourcing .bashrc? On OS X, you normally have a login shell. – fd0 – 2015-08-04T10:08:35.997

@bertieb, I am not trying to change the histfile. commenting out the lines from .bashrc does not change anything in terms of output. $HISTFILESIZE and $HISTSIZE still output 500 because I believe that is the mac default. – None – 2015-08-04T19:12:36.950

@fd0, I'm not sure what your question means, could you clarify? What's the relationship between .bashrc and a login shell? – None – 2015-08-04T19:13:52.340

1Always 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 – 2015-12-30T12:48:37.587

I was experiencing this same problem because my .bash_profile was invoking an osascript to quit the terminal process after the last visible window/tab closed. Somehow that script was preventing the history from being stored in my history file when the terminal exited. – axiopisty – 2019-10-16T18:14:45.693

Answers

26

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

diimdeep

Posted 2015-08-04T09:18:49.423

Reputation: 762

3But 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 – 2015-12-30T12:41:43.337

1@ChrisPage Actually Apple changed some bits of script. Updated answer, thanks. – diimdeep – 2015-12-30T15:07:34.447

@diimdeep Where would I append this line? export SHELL_SESSION_HISTORY=0 – zerohedge – 2017-05-14T00:23:41.950

@zerohedge .bashrc and .bash_profile https://unix.stackexchange.com/a/310150/15362

– diimdeep – 2017-05-14T06:57:30.693

@diimdeep Thank you. This seems to be working right now. Does it have any ramifications? – zerohedge – 2017-05-14T13:52:16.527

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.

rabs

Posted 2015-08-04T09:18:49.423

Reputation: 161

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 – 2015-12-30T12:44:24.527

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 – 2016-02-25T00:53:08.040

@rabs I'd suggest adding SHELL_SESSION_HISTORY=0 at the top of ~/.bash_profile. – Teejay – 2017-12-27T16:18:07.420

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.

Alex Rojo

Posted 2015-08-04T09:18:49.423

Reputation: 51

1Nice fix, without having to modify rvm. – mlo55 – 2017-08-08T06:31:36.360

1

Are we talking about the Ruby enVironment Manager (RVM) https://rvm.io ?? When and why should that become involved?

– MarkHu – 2019-06-03T20:48:07.990

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"

noio

Posted 2015-08-04T09:18:49.423

Reputation: 186

This indeed helped! – Karsten – 2017-03-19T21:06:54.690

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.

theoden8

Posted 2015-08-04T09:18:49.423

Reputation: 644

I've edited .profile so it is the same as my .bashrc, and history still doesn't come up. – None – 2015-08-07T18:18:02.040

@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 – 2015-08-07T18:52:57.533

how would I echo $HISTFILE in different parts of profile? – None – 2015-08-08T19:12:05.147

@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 – 2015-08-08T19:16:32.813

Hi @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 an echo $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. – None – 2015-08-09T04:52:18.350

@NelsonLiu, if it is really like this, you should 1) make .bash_profile same as .profile 2) do as I said before. – theoden8 – 2015-08-09T15:45:09.970

1so 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. – None – 2015-08-12T18:06:34.297

@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 your bash_profile for sure: sleep 2 && export HISTFILE=~/.bash_history &. – theoden8 – 2015-08-13T16:32:59.673

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

Julian Jordan

Posted 2015-08-04T09:18:49.423

Reputation: 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 – 2019-06-03T21:15:03.427