Stop the shell history from getting nuked when you run multiple sessions
Environment & Shell Customization (Aliases/Functions/.Bashrc)
Stop the shell history from getting nuked when you run multiple sessions
🧩 The Challenge
Opening three terminal windows usually means your shell history from the first one gets overwritten by the last one you closed. I’ve lost so many clever one-liners because of this default race condition.
💡 The Fix
Change your bash settings so it appends history to the file immediately rather than clobbering it on logout. It makes tracking your commands across concurrent sessions actually possible.
shopt -s histappend
PROMPT_COMMAND="history -a; history -n; $PROMPT_COMMAND"
⚙️ Why It Works
Adding that shopt makes bash append instead of overwrite, while the custom PROMPT_COMMAND forces the shell to sync your disk-based history file every single time you hit enter.
🚀 Pro-Tip: Use ‘history -a’ manually in a stalled session if you really need to force a write before shutting down the host.
Linux Tips & Tricks | © ngelinux.com | 10/9/2026
