Site icon New Generation Enterprise Linux

Stop letting your bash history get overwritten by multiple terminal windows

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

Stop letting your bash history get overwritten by multiple terminal windows

🧩 The Challenge

You know that feeling when you run a command in one window, only to realize your main terminal session wiped it out because it closed last? It is incredibly infuriating to lose your exact command history just because you had three sessions open at once.

💡 The Fix

Change your shell behavior to append commands to the history file immediately rather than overwriting it when the session closes. This keeps all your tabs in sync so you don’t lose that one obscure grep command you just perfected.

shopt -s histappend
PROMPT_COMMAND="history -a; history -n; $PROMPT_COMMAND"

⚙️ Why It Works

By telling the shell to append instead of truncate and forcing a read/write operation every time the prompt displays, you ensure every terminal session shares the exact same state. It effectively turns your disparate history buffers into one unified stream.

🚀 Pro-Tip: Add HISTSIZE and HISTFILESIZE to your config as well so you don’t lose the older stuff as you accumulate new commands.

Linux Tips & Tricks | © ngelinux.com | 8/20/2026

0 0 votes
Article Rating
Exit mobile version