How to persist bash history?

9

3

I don't know if this is expected, but my history is not saved across sessions. This is to say, if I close the window, then when I open it again, the history is empty. How can I persist it across sessions ?

Here are the outputs of the commands you asked:

 set -o | grep history
history         on

$ grep -i history ~/.bashrc ~/.bash_profile ~/etc/bash.bashrc ~/etc/profile ~/.profile
/cygdrive/c/cygwin/home/car/.bashrc:# Make bash append rather than overwrite the history on disk
/cygdrive/c/cygwin/home/car/.bashrc:# History Options
/cygdrive/c/cygwin/home/car/.bashrc:# Don't put duplicate lines in the history.
/cygdrive/c/cygwin/home/car/.bashrc:# export PROMPT_COMMAND="history -a"
grep: /cygdrive/c/cygwin/home/car/etc/bash.bashrc: No such file or directory
grep: /cygdrive/c/cygwin/home/car/etc/profile: No such file or directory
/cygdrive/c/cygwin/home/car/.profile:if [ "x$HISTFILE" == "x/.bash_history" ]; then
/cygdrive/c/cygwin/home/car/.profile:  HISTFILE=$HOME/.bash_history

$ ls -la ~/ | grep history -> no output

$ echo $HISTFILE 
~/.bash_history
$ echo $HISTSIZE
500
$ echo $HISTFILESIZE 
500

After the edits described in the answer below, I now get:

grep -i hist .bashrc
# Make bash append rather than overwrite the history on disk
shopt -s histappend
# History Options
# Don't put duplicate lines in the history.
export HISTCONTROL="ignoredups"
# (added) A new shell gets the history lines from all previous shells
PROMPT_COMMAND='history -a'
# HISTIGNORE is a colon-delimited list of patterns which should be excluded.

I am still unable to have a history saved across sessions. I read the following questions:

None seemed to address my issue, including the answer below from the very person which had their question answered from the supposed duplicate.

Car981

Posted 2013-04-18T12:12:25.087

Reputation: 305

Is this still an issue? If it is, try adding the relevant lines to ~/.profile istead of .bashrc. I'm not sure how cygwin is set up but it might be running login shells in which case .bashrc is ignored and .profile is read instead. – terdon – 2015-01-25T00:40:50.413

Also see my question here and the questions it links to. Try the suggestions in them and ask again if you still have problems. In the emantime, voting to close as a duplicate.

– terdon – 2013-04-18T12:39:57.183

edit: set -o | grep history shows the history is on, echo $HISTFILE shows ~/.bash_history, echo $HISTSIZE and $HISTFILESIZE both show 500. But the history is still not persisted across sessions – Car981 – 2013-04-18T13:05:23.723

The $HISTFILE is set to ~/.bash_history, but there is no .bash_history file in ~. How can this be ? – Car981 – 2013-04-18T13:20:57.647

Please [edit] your question to add new information rather than commenting. Include the output of these commands: set -o | grep history, grep -i history ~/.bashrc ~/.bash_profile ~/etc/bash.bashrc ~/etc/profile ~/.profile and ls -la ~/ | grep history. – terdon – 2013-04-18T13:41:35.213

Try setting a specific history file, add this line to your ~/.bashrc: export HISTFILE="~/history". Does that make a difference? – terdon – 2013-04-19T12:38:00.510

Answers

7

Well, it looks like your ~/.bashrc does not have the necessary options. Make sure these lines are in your ~/.bashrc:

# Make Bash append rather than overwrite the history on disk:
shopt -s histappend
# A new shell gets the history lines from all previous shells
PROMPT_COMMAND='history -a'
# Don't put duplicate lines in the history.
export HISTCONTROL=ignoredups

terdon

Posted 2013-04-18T12:12:25.087

Reputation: 45 216

It still doesn't work. See edit for an update of the new output result. – Car981 – 2013-04-18T15:21:13.177

7

OK I found out what's wrong. I can't close the window, I have to type 'exit' for it to close gracefully.

Car981

Posted 2013-04-18T12:12:25.087

Reputation: 305

You have answered your own question! This is great, but you should mark it as such, for others to know that you are not expecting any further answers. – Tiago – 2015-03-14T15:25:08.910

Hard to say this is an acceptable answer - I've got the same issue and manually closing the window to save history is not a good idea. Often, I close the terminal window in other ways and would not like to loose it then. – Krzysztof Bociurko – 2018-09-03T13:01:58.617