Stop your shell history from dying every time you open a new terminal window

Environment & Shell Customization (Aliases/Functions/.Bashrc)

Stop your shell history from dying every time you open a new terminal window

🧩 The Challenge

Everyone has had that moment where they run a complex command in one terminal, only to realize the history entry vanished because they opened another window first. It’s infuriating when your session overwrites your hard-earned command history instead of appending to it.

💡 The Fix

You need to tell bash to append to the history file immediately rather than overwriting it when the session closes. This makes sure your commands persist even if your terminal crashes or you have a dozen sessions running at once.

shopt -s histappend
PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

⚙️ Why It Works

Setting histappend ensures new lines are tacked onto the end of the file, while that PROMPT_COMMAND hack forces the shell to sync your current session’s history with the disk every time you hit enter. It keeps all your windows in lockstep so you never lose that one perfect regex you spent ten minutes crafting.

🚀 Pro-Tip: Set your HISTSIZE and HISTFILESIZE to something massive like 50000 so you never have to worry about your older commands rolling off the edge.

Linux Tips & Tricks | © ngelinux.com | 7/28/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted