How do I keep my bash history across sessions?

28

12

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?

BЈовић

Posted 2010-11-17T14:51:32.040

Reputation: 405

He 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. – Jonathan Leffler – 2010-11-17T15:00:16.797

Good point—this probably should be moved to SuperUser. – B.R. – 2010-11-17T15:02:11.367

@Jonathan Yes, you got the question correct. I wasn't sure what exactly to ask. – None – 2010-11-17T16:02:05.220

Saving 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' – Abhinav – 2013-08-04T22:05:15.853

Answers

17

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).

B.R.

Posted 2010-11-17T14:51:32.040

Reputation: 286

7HISTSIZE is the number of commands to remember, not the number of bytes. – Chris Down – 2014-06-12T03:04:01.223

10

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.

icc97

Posted 2010-11-17T14:51:32.040

Reputation: 396

same here - ended several days of wandering :) thanks! – davka – 2017-03-05T12:16:00.730

1sudo chmod your_user_name .bash_history did it! Thanks. – wij – 2017-08-22T05:33:54.260

sudo chown user_name:user_name ~.bash_history solved it! – alper – 2018-08-08T19:01:28.103

3I 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 :-) – SamStephens – 2013-07-16T16:47:40.813

3

Look up the environment variables HISTFILE, HISTSIZE, HISTFILESIZE.

Jonathan Leffler

Posted 2010-11-17T14:51:32.040

Reputation: 4 526

2

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

simotek

Posted 2010-11-17T14:51:32.040

Reputation: 205

1

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

mxmlnkn

Posted 2010-11-17T14:51:32.040

Reputation: 151

0

My Amazon EC2 Ubuntu instance was not saving the history. .bashrc had all the required commands as mentioned in other answers but the only thing that was missing was the file. I had to touch it to make it work.

touch ~/.bash_history

Chankey Pathak

Posted 2010-11-17T14:51:32.040

Reputation: 215

0

I would ensure .bash_history exists. If it does not, there's nowhere for bash to save command history.

if .bash_history does not exist create it by running touch:

touch ~/.bash_history

this just creates the empty .bash_history file in your home directory, which will fill up with your commands, and should persist between sessions.

Ray Shah

Posted 2010-11-17T14:51:32.040

Reputation: 1

-1

DanJ

Posted 2010-11-17T14:51:32.040

Reputation: 311

// , 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? – Nathan Basanese – 2018-01-08T01:05:23.833

3Welcome 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). – Indrek – 2012-05-16T00:46:05.110