How to remove a secret from .bash_history on Mac

Safely scrub sensitive tokens and export statements from Bash history files on macOS.

1. The Problem: In-Memory Buffer Overwrites

Like Zsh, Bash retains history in memory during an active terminal session. If you edit ~/.bash_history while a bash terminal is open, exiting the terminal appends the in-memory buffer back to disk, restoring the secret.

2. Step-by-Step Fix

Step 1: Clear the in-memory history for your current session:

history -c

Step 2: Create a backup of your history file:

cp ~/.bash_history ~/.bash_history.bak

Step 3: Delete the line containing the secret using BSD sed:

sed -i '' '/your_secret_key_here/d' ~/.bash_history

Step 4: Reload the history into your active shell:

history -r

Related Guides