Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

migrated from Nov 17 '10 at 17:43

This question came from our site for professional and enthusiast programmers.

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 Nov 17 '10 at 15:00
Good point—this probably should be moved to SuperUser. – B.R. Nov 17 '10 at 15:02
@Jonathan Yes, you got the question correct. I wasn't sure what exactly to ask. – VJo Nov 17 '10 at 16:02
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 Aug 4 '13 at 22:05

7 Answers 7

up vote 10 down vote accepted

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

share|improve this answer
HISTSIZE is the number of commands to remember, not the number of bytes. – Chris Down Jun 12 '14 at 3:04

Look up the environment variables HISTFILE, HISTSIZE, HISTFILESIZE.

share|improve this answer

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.

share|improve this answer
I 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 Jul 16 '13 at 16:47

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:

share|improve this answer

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.

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
# --reverse lists newest files at first
names=($(ls --reverse $HISTFOLDER/*.$HISTFILEEXT 2>/dev/null))
# 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
# Beginning with the oldest load files in $toload into bash history buffer
for (( i=${#toload[*]}-1; i>=0; i-- )); do
    history -r ${toload[$i]}
share|improve this answer

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.

share|improve this answer

Good answer to this question is here:

share|improve this answer
Welcome 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 May 16 '12 at 0:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.