How to persist bash history?
Solution 1
OK I found out what's wrong. I can't close the window, I have to type 'exit' for it to close gracefully.
Solution 2
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
Solution 3
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"
}
Related videos on Youtube
Car981
Updated on September 18, 2022Comments
-
Car981 over 1 year
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:
- Bash history loss when using histappend
- How to prevent Bash from altering history?
- What determines what shows up in the bash history command?
- How do I keep my bash history across sessions?
- save bash history, regularly
None seemed to address my issue, including the answer below from the very person which had their question answered from the supposed duplicate.
-
Car981 about 11 yearsIt still doesn't work. See edit for an update of the new output result.
-
Tiago about 9 yearsYou 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.
-
Krzysztof Bociurko over 5 yearsHard 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.
-
himanshuxd almost 4 yearssame thing has been happening to me.