Stop living in a shell history blackout after a session crash

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

Stop living in a shell history blackout after a session crash

🧩 The Challenge

You’ve finally tracked down that weird edge-case bug and typed out the perfect long-form command, but then your SSH session hangs or your terminal emulator decides to die. When you log back in, that command you spent five minutes refining is nowhere to be found in your history file.

💡 The Fix

Change your bash configuration to append to the history file after every single command instead of waiting for the session to close cleanly. You’ll never lose that golden line of shell history again, even if the server reboots or your client drops the connection.

echo 'PROMPT_COMMAND="history -a; $PROMPT_COMMAND"' >> ~/.bashrc

⚙️ Why It Works

Setting this forces the shell to flush the current session’s history buffer to the disk immediately after the command executes. By piggybacking on the PROMPT_COMMAND variable, you guarantee that every line you run is safely committed to your history file before you even hit enter on the next one.

🚀 Pro-Tip: Pair this with HISTCONTROL=ignoredups:erasedups to keep your history clean without losing the recent stuff you actually need.

Linux Tips & Tricks | © ngelinux.com | 10/4/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted