How do I keep my bash history across sessions?
Solution 1
Which history? bash-history? If you're losing bash history and you have multiple sessions at a time, it's because each session is overwriting the other sessions' history.
You probably want to tell bash to not overwrite the history each time, but rather to append to it. You can do this by modifying your .bashrc to run shopt -s histappend
.
You can also increase the size of your history file by exporting HISTSIZE to be a large-ish number (it's in bytes, so 100000 should be plenty).
Solution 2
I was suffering from the same problem - but my .bashrc
file already had the shopt -s histappend
and correct HISTFILE
, HISTSIZE
, HISTFILESIZE
.
For me the problem was that my .bash_history
file was owned by root rather than my username, so my user could never save to that file on exit.
Solution 3
I have written a script for setting a history file per session or task its based off the following.
# write existing history to the old file
history -a
# set new historyfile
export HISTFILE="$1"
export HISET=$1
# touch the new file to make sure it exists
touch $HISTFILE
# load new history file
history -r $HISTFILE
It doesn't necessary save every history command but it saves the ones that i care about and its easier to retrieve them then going through every command. My version also lists all history files and provides the ability to search through them all.
Full source: https://github.com/simotek/scripts-config/blob/master/hiset.sh
Solution 4
Look up the environment variables HISTFILE, HISTSIZE, HISTFILESIZE.
Solution 5
I just added this to my ~/.bashrc
and ~/.profile
and that took care of it.
HISTSIZE=5000
HISTFILESIZE=10000
HISTFILE="/Users/jdoe/.bash_history"
export HISTSIZE HISTFILESIZE HISTFILE
echo HISTSIZE is $HISTSIZE
echo HISTFILESIZE is $HISTFILESIZE
echo HISTFILE is $HISTFILE
Related videos on Youtube
![BЈовић](https://i.stack.imgur.com/v9mVv.jpg?s=256&g=1)
BЈовић
Drink milk, it is healthier then oil! If you want to see how I see the Presenter first's implementation in Qt, take a look into my pet project.
Updated on September 17, 2022Comments
-
BЈовић almost 2 years
I am working on a x86 target running fedora 9.
Whenever I reboot it, my history returns to some state, and I do not have commands I did in the sessions before the reboot.
What I have to change to have updated history I had before reboot?
-
joaquin over 13 yearsHe is asking "How do I keep my bash history across sessions?", which is related to shell programming. The reboot is a dramatic way of losing your shell, that's all. It doesn't need closing off topic.
-
samiaj over 13 yearsGood point—this probably should be moved to SuperUser.
-
Admin over 13 years@Jonathan Yes, you got the question correct. I wasn't sure what exactly to ask.
-
Abhinav almost 11 yearsSaving each command right after it has been executed, not at the end of the session will also help. This is because if you are running two sessions simultaneously, then without the line below, bash will save the history from session-1 (say) when its closed. If session-1 is running and you want to immediately access the history of session-1 inside session-2, then you wont be able to unless you add the below line to the .bashrc file. PROMPT_COMMAND='history -a'
-
-
Indrek about 12 yearsWelcome to SuperUser! Please include enough information in your answer that the asker doesn't have to click on an external link. Also, make sure your answer doesn't simply duplicate existing answers (which, from a quick glance at that link, it seems to be doing).
-
SamStephens almost 11 yearsI just found I had exactly the same problem with .bash_history owned by root. I wish I'd realized before I'd lost all my lovely history, but nevermind :-)
-
Chris Down about 10 years
HISTSIZE
is the number of commands to remember, not the number of bytes. -
davka over 7 yearssame here - ended several days of wandering :) thanks!
-
wij almost 7 years
sudo chmod your_user_name .bash_history
did it! Thanks. -
Nathan Basanese over 6 years// , Looks like a good article. But I think stackexchange is trying to be independent of link destinations, at least a little. Mind adding a little explanation?
-
alper almost 6 years
sudo chown user_name:user_name ~.bash_history
solved it! -
Cameron Kerr about 4 yearsInteresting; bash on RHEL6 (bash 4.1 series) has this off by default, while bash on RHEL7 (bash 4.2 series) has this on by default.