13

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.

6
  • 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
    Apr 18, 2013 at 12:39
  • 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
    Apr 18, 2013 at 13:05
  • The $HISTFILE is set to ~/.bash_history, but there is no .bash_history file in ~. How can this be ?
    – Car981
    Apr 18, 2013 at 13:20
  • 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
    Apr 18, 2013 at 13:41
  • Try setting a specific history file, add this line to your ~/.bashrc: export HISTFILE="~/history". Does that make a difference?
    – terdon
    Apr 19, 2013 at 12:38

4 Answers 4

11

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

4
  • 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. Mar 14, 2015 at 15:25
  • 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. Sep 3, 2018 at 13:01
  • same thing has been happening to me.
    – himanshuxd
    Jul 4, 2020 at 8:44
  • It's not clear to me what you exactly want to achieve with persistent history. Assume that you have several terminals (or terminal tabs) open, each running bash, with their own history. Which is the one which should be saved in the end? Oct 10, 2022 at 8:02
8

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
1
  • It still doesn't work. See edit for an update of the new output result.
    – Car981
    Apr 18, 2013 at 15:21
1

I would highly recommend this setup in your bash config. It saves each command into a file which is named the current date, i.e. bash-history-2020-10-29.log in the ~/.logs directory.

# Saving history to file
export PROMPT_COMMAND='if [ "$(id -u)" -ne 0 ]; then echo "$(date "+%Y-%m-%d.%H:%M:%S") $(pwd) $(history 1)" >> ~/.logs/bash-history-$(date "+%Y-%m-%d").log; fi'

export HISTSIZE=100000
export HISTTIMEFORMAT="%d/%m/%y %T  "
# Avoid duplicates
export HISTCONTROL=ignoredups:erasedups  
# When the shell exits, append to the history file instead of overwriting it
#shopt -s histappend

# After each command, append to the history file and reread it
export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"

Then using this bash function allows on to query and sort by date for previously used shell inputs.

alias s='search'
search() {
   ls -rt ~/.logs/*.log | xargs grep -rnw "$1"
}
0

If you want a more complete solution that persists your bash history locally, stores additional context (cwd, runtime, exit code, etc) and syncs it to your other computers, see hishtory. I got frustrated with this exact problem and built it to hopefully make shell histories work better. It works by storing a separate copy of your shell history in a SQLite DB where the many quirks of bash history handling can't accidentally delete it.

3
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Oct 9, 2022 at 18:56
  • It seems like this is self-promotion. Please read the article about expected behaviour here. Oct 9, 2022 at 18:59
  • Sorry about that! I updated it to make my affiliation clear and to include the relevant details on how it works.
    – David D
    Oct 11, 2022 at 4:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .