How do I keep my bash history across sessions?
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?
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:
Look up the environment variables HISTFILE, HISTSIZE, HISTFILESIZE.
Solution 4:
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 5:
I wrote some lines in my .bashrc which accomplish the following: Save every session after each command to a file. There will be as many history files as you have ever started terminals. On starting a new terminal beginning from the most recent history file load all history files from prior sessions into the history buffer until a line count threshold is reached.
HISTCONTROL=''
HISTFOLDER=~/.bash_histories
HISTFILEEXT=history # only files in $HISTFOLDER with this extension will be read
shopt -s histappend # append when closing session
mkdir -p $HISTFOLDER
HISTFILE=$HISTFOLDER/$(date +%Y-%m-%d_%H-%M-%S_%N).$HISTFILEEXT # create unique file name for this session. Nanoseconds seems to be unique enough, try: for ((i=0; i<=10; i++)); do date +%Y-%m-%d_%H-%M-%S_%N; done
# if HISTFILE unset, history is not saved on exit -> not really necessary if we save after each command, but its a double net safety
HISTSIZE=-1 # maximum number of commands to hold inside bash history buffer
HISTFILESIZE=-1 # maximum number of lines in history file
# history -a $HISTFILE # bash saves the total history commands entered since startup or since the last save and saves that amount of commands to the file. This means reading a history file after typing commands will trip up bash, but this won't be a problem if the history file is only loaded in the beginning. This means that only new commands are saved not all the old loaded commands, thereby we can load as many history files into the buffer as we want and still only save newly thereafter typed commands
PROMPT_COMMAND="history -a $HISTFILE; $PROMPT_COMMAND" # This command is executed after very typed command -> save history after each command instead after only closing the session
# Load old histories from last 5 files/sessions
HISTLINESTOLOAD=2000
# --reverse lists newest files at first
names=($(ls --reverse $HISTFOLDER/*.$HISTFILEEXT 2>/dev/null))
toload=()
linecount=0
# Check if is really file and count lines and only append to $toload if linecount under $HISTLINESTOLOAD
for fname in ${names[*]}; do
if test -f $fname; then
linecount=$((linecount+$(wc -l < $fname) ))
if test $linecount -ge $HISTLINESTOLOAD; then
break
fi
toload+=($fname)
fi
done
# Beginning with the oldest load files in $toload into bash history buffer
for (( i=${#toload[*]}-1; i>=0; i-- )); do
history -r ${toload[$i]}
done