In my computer at work, my bash shell remembers history from previous sessions, even if I just opened a new terminal.
In my computer at home, this doesn't happen.
I have the same OS 10.6.8 in both machines and I don't have any history related setting in .bash_profile
.
How could I set this up on my home computer so it remembers history across sessions?
-
Also asked at superuser.com/q/421544/4714– glenn jackmanMay 7, 2012 at 14:53
-
Best answer: unix.stackexchange.com/questions/1288/…– mlesMar 22, 2016 at 21:01
6 Answers
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
-
5That should all be unnecessary by default. The default bash configuration (at least on Mac OS X) enables command history. May 8, 2012 at 8:50
-
Key word, /should/. If his isn't recording history, this will force bash to keep the history on init. May 8, 2012 at 12:29
-
4Not merely should, but will unless something has been done to suppress the default history behavior, in which case, just slamming some values into these variables is merely papering over the actual problem. If this answer resolves the issue, then something else is really messed up and should be fixed to avoid other issues. Dec 30, 2015 at 12:23
-
You can set up these settings, but I found a simpler solution/answer if you just want to "enable" the history on terminal. It just worked fine for me apple.stackexchange.com/a/220667/243988 (I am using macOS High Sierra)– jpruizsJul 10, 2019 at 23:09
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
-
Disabling the per-session command history—the default behavior—is the opposite of what's being asked. Jun 13, 2017 at 2:01
-
@ChrisPage That's only correct if the questioner is asking about per-session histories, which doesn't seem to be the case ("remember history across sessions"). Disabling bash session behavior ENABLES replaying a single history in
.bash_history
rather than keeping the history under the.bash_sessions/
folder. This is the correct answer imho.– Matt HNov 10, 2017 at 19:43 -
@MattH The question asks how to get history working again, because it seems to be not working at all, and disabling this per-session history feature isn't going in the right direction. If there's a bug in the per-session history mechanism, someone should file a bug report, but presenting an answer as though turning it off is somehow generally correct for all users is…incorrect. It normally works just fine. See my comments on the various answers for more information. Nov 16, 2017 at 9:00
-
I should have also mentioned this back when I initially commented: disabling the entire bash save/restore mechanism is meant as a last resort and should be avoided if possible. You can merely turn off the per-session command history behavior by setting
SHELL_SESSION_HISTORY
to 0, as documented in/etc/bashrc_Apple_Terminal
. Nov 16, 2017 at 9:05
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"
I had a similar problem as well, no history recorded. The given solutions didn't work, but at long last I found out that the problem was as simple as a missing .bash_history
The solution was to go to /home/[username]/
and add a file .bash_history
, so
nano .bash_history
and then type anything in the file (otherwise it won't be created), exit and save. After this the history started functioning normally.
-
If this resolves the issue, then something else is wrong and should be fixed to avoid other issues. If you start up Bash when there is no history file, it will create one. If it doesn’t try to create one, or it is unable to create it, there’s something wrong with some shell startup script or the home directory permissions. For example, see the answer: apple.stackexchange.com/a/118764/6883 Dec 30, 2015 at 12:25
If you add shopt -s histappend
to your ~/.bash_profile
then even with history sessions enabled (which it is by default in MacOS), the primary ~/.bash_history
file will be appended with the results of each session. My ~/.bash_profile
is configured as follows:
export HISTSIZE=100000000 # yes I know this is big I manage it manually
export HISTFILESIZE=100000000 # yes I know this is big I manage it manually
shopt -s histappend
This answer applies to <= MacOS 10.15.x