Accidentally pasting API keys, database passwords, or secret tokens into a terminal prompt stores them in plain text inside your shell history file (~/.bash_history). Completely purging shell history requires clearing both active in-memory buffer history and deleting on-disk history files.

Complete History Purge Commands

Terminal History Cleanup Commandsbash
# 1. Truncate on-disk bash history file to 0 bytes
cat /dev/null > ~/.bash_history
 
# 2. Clear current in-memory shell history buffer
history -c
 
# 3. Write empty history buffer to disk
history -w

Prevent Logging Sensitive Commands (Leading Space Trick)

Set HISTCONTROL=ignorespace in your ~/.bashrc. Typing any shell command with a leading space prevents Bash from recording it to history:

~/.bashrcbash
export HISTCONTROL=ignorespace
 
# Executing command with a LEADING SPACE:
 $ export MYSQL_PASSWORD="secret_password_123" # Not logged in history!