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?

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

9 Answers 9


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

  • 9
    HISTSIZE is the number of commands to remember, not the number of bytes.
    – Chris Down
    Jun 12, 2014 at 3:04
  • Interesting; bash on RHEL6 (bash 4.1 series) has this off by default, while bash on RHEL7 (bash 4.2 series) has this on by default. Jun 3, 2020 at 23:16
  • You also need to add: PROMPT_COMMAND='history -a' Aug 30, 2023 at 15:36

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.

  • 4
    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 :-) Jul 16, 2013 at 16:47
  • same here - ended several days of wandering :) thanks!
    – davka
    Mar 5, 2017 at 12:16
  • 2
    sudo chmod your_user_name .bash_history did it! Thanks.
    – wij
    Aug 22, 2017 at 5:33
  • 1
    sudo chown user_name:user_name ~.bash_history solved it!
    – alper
    Aug 8, 2018 at 19:01

Look up the environment variables HISTFILE, HISTSIZE, HISTFILESIZE.


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


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]}

I just added this to my ~/.bashrc and ~/.profile and that took care of it.



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.


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

Good answer to this question is here:


  • 4
    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, 2012 at 0:46
  • // , 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? Jan 8, 2018 at 1:05

You must log in to answer this question.

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