Terminal history not saving between sessions on Linux? Fix it now

Your terminal history disappears when you close the shell. Usually it's a missing .bash_history file or wrong permissions. Here's how to fix it for good.

Yeah, that's annoying. You type a bunch of commands, close the terminal, open it again, and poof — nothing. Like you never worked. Had a client last week whose whole DevOps pipeline setup got wiped because of this. Let's fix it.

The quick fix (works 90% of the time)

Open a terminal and run this:

chmod 600 ~/.bash_history
echo 'export HISTSIZE=10000' >> ~/.bashrc
echo 'export HISTFILESIZE=20000' >> ~/.bashrc
echo 'shopt -s histappend' >> ~/.bashrc
source ~/.bashrc

Now close and reopen your terminal. Type a couple commands, close it, open it again, and type history. They should be there. If not, read on.

Why this works

Three things go wrong most of the time:

  • File permissions — your .bash_history file might be owned by root or have wrong permissions. The chmod 600 ensures only you can read/write it. If the file doesn't exist, it gets created when the shell exits.
  • History size — default HISTSIZE is often 500 or 1000. That fills up fast if you're a heavy user. Bump it to 10000 or more.
  • Append not overwrite — without shopt -s histappend, each new terminal session overwrites the history file. So the last one to close wins. That's why you lose earlier commands.

The histappend setting tells bash to add new commands to the file instead of replacing it. Big difference.

Less common variations of the same issue

History file corrupt

Sometimes the file gets binary junk in it. Check it with:

file ~/.bash_history

If it says something other than "ASCII text", delete the file and let bash create a new one:

rm ~/.bash_history
history -c

Then log out and back in.

Multiple terminals fighting over the file

If you open 5 terminals and close them all at once, the last one to write wins. To fix that, add this to your .bashrc:

export PROMPT_COMMAND="history -a; history -c; history -r"

This writes each command immediately (-a), clears the in-memory history (-c), then reloads from the file (-r). Now every terminal sees everything every other terminal typed. Works great.

No .bashrc at all

Some minimal systems (Docker containers, Alpine, busybox) don't have a .bashrc by default. Create one:

touch ~/.bashrc
echo 'export HISTFILE=~/.bash_history' >> ~/.bashrc

Also check that ~/.bash_profile or ~/.profile sources .bashrc. Add this line to ~/.profile if it's missing:

if [ -f ~/.bashrc ]; then . ~/.bashrc; fi

Zsh or Fish users

If you're using zsh, the history file is usually ~/.zsh_history. Same permissions fix applies. For fish, it's handled differently — check ~/.local/share/fish/fish_history.

Root user history

If you su to root and lose history, that's because root's .bash_history is separate. You can either fix root's settings too, or use sudo -s to preserve your user's environment.

Prevention — set it and forget it

Once you've got the fix working, make sure it stays. Add these lines to ~/.bashrc:

# History settings
export HISTSIZE=10000
export HISTFILESIZE=20000
export HISTTIMEFORMAT='%F %T '
export HISTCONTROL=ignoredups
export HISTFILE=~/.bash_history
shopt -s histappend
shopt -s cmdhist
PROMPT_COMMAND="history -a; history -c; history -r"

I always set HISTTIMEFORMAT too — adds timestamps so you know when you ran that scary command last week. ignoredups skips duplicates so your history doesn't fill with ls over and over.

One last thing: if you're on a shared machine, set HISTFILE to somewhere only you can write, like ~/.bash_history. I had a client whose history file was in /tmp — gone every reboot. Don't do that.

That's it. Your terminal history should now behave like it has a memory. Type away.

Related Errors in Linux & Unix
Fix 'Permission denied' for files you own on Linux Stuck at GRUB? Recovery Mode Won't Load – Fix It GDM3 failed to start GDM3 Won't Start? Here's the Real Fix Fix 'command not found' in Linux: PATH and alternatives

Was this solution helpful?

EP
Erropedia Team
Tech Support Editors
The Erropedia editorial team researches and documents real-world tech errors from across Windows, Linux, macOS, networking, databases, cloud platforms, and more. Every solution is reviewed for accuracy and updated as software and systems evolve.