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
# 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 -wPrevent 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:
export HISTCONTROL=ignorespace
# Executing command with a LEADING SPACE:
$ export MYSQL_PASSWORD="secret_password_123" # Not logged in history!
Comments and corrections