Your shell history may contain API keys
export API_KEY=... typed last month can still be in that plain text file.
Published here , updated · 4 sources · Resume reading
On macOS, zsh writes your commands to ~/.zsh_history when the session closes (or sooner if you share history). So export API_KEY=... typed last month can still sit in that plain text file, readable by anything that runs as you [1][2]. Grep it, rotate real hits, then stop new exports from landing there.
One minute: find suspicious lines
Use a starting pattern. Add the prefixes your own keys use. Never paste the output into chat, tickets, or screenshots [4].
grep -nE 'sk-|ghp_|AKIA|_KEY=' ~/.zsh_historyOn this host a throwaway zsh-format file with a fake key printed:
printf '%s\n' \
': 1700000000:0;ls' \
': 1700000001:0;export API_KEY=fake123sk-test' \
': 1700000002:0;echo hello' \
> /tmp/hist-demo/zsh_history
grep -nE 'sk-|ghp_|AKIA|_KEY=' /tmp/hist-demo/zsh_historyCaptured output:
2:: 1700000001:0;export API_KEY=fake123sk-testUsing Apple's Terminal? It also keeps per-session copies under ~/.zsh_sessions. Search those too:
grep -rnE 'sk-|ghp_|AKIA|_KEY=' ~/.zsh_sessionsAfter finding a real key
Rotate it with the provider. That is the fix. Deleting the history line alone does not undo a leak that already left the machine. Then scrub local history files and any synced history database so the next laptop restore does not resurrect the string.
Stop new ones: ignore space-prefixed commands
Zsh option HIST_IGNORE_SPACE skips commands that begin with a space [2]. Add it to ~/.zshrc:
setopt HIST_IGNORE_SPACEThen start any secret command with a leading space. Bash uses HISTCONTROL=ignorespace for the same idea [4].
# demo with a throwaway HISTFILE (Bash 5.2 on this host)
export HISTFILE=/tmp/hist-demo/demo_history
: > "$HISTFILE"
set -o history
HISTCONTROL=ignorespace
history -c
history -s 'ls'
history -s ' export SECRET=spaceprefixed'
history -s 'export VISIBLE=plain'
history -w
cat -A "$HISTFILE"Captured output (space-prefixed secret absent; visible export present):
ls$
export VISIBLE=plain$
history -w$Prefer prompting over exporting
read -s keeps the value out of the typed command line [3][4]:
read -s API_KEY && export API_KEYThe value still lives in the process environment afterward. That is expected for the session. The win is that the literal secret never became a history entry.
A typed export becomes a durable history line; space ignore or silent read keeps the literal out of the file.
Timing quirks that bite
A space-prefixed line can still sit in the open session's in-memory history until the next command or until the shell writes the file [2][3]. Do not assume the secret is gone the moment you typed the space.
If you set INC_APPEND_HISTORY or SHARE_HISTORY in Zsh, lines are written as you type them, not only at exit [2]. That is good for multi-terminal sync and bad for "I will clean it up later."
Tools like Atuin keep their own history database. After a leak, search that store the same way you search ~/.zsh_history.
Bash users: HISTCONTROL=ignorespace (or ignoreboth, which includes ignorespace) does the ignore-space job [4]. HISTFILE still points at a plain text path; locking down home directory permissions does not encrypt that file.
Example walkthrough with a fake key
On a throwaway history file, a plain export API_KEY=fake123 lands on a greppable line after the shell writes history. With ignore-space enabled and a leading space, the same pattern finds nothing for that secret. With read -s, the value never reaches the file as typed text. Use only fake values while practicing.
Mistakes that leave keys on disk
Typing export with a real production token "just for a minute" in a shared screen recording.
Grepping history and pasting the matching lines into Slack for help.
Deleting one line while leaving INC_APPEND_HISTORY copies and Terminal session files untouched.
Setting HIST_IGNORE_SPACE but forgetting the leading space on the command that mattered.
Rotating the cloud key but leaving the old string in Atuin or a backup of ~.
Treating browser private mode as coverage for the terminal history file.
What the history file actually is
Zsh stores history as a plain text file whose path comes from HISTFILE (defaulting under your home directory on typical Mac setups) [1]. Bash does the same with its own HISTFILE default [4]. There is no encryption, no per-line ACL, and no special kernel protection beyond ordinary file modes. If your home directory is readable by another local account, or if a backup agent copies $HOME to a shared disk, those lines travel with it.
Session helpers make this worse in quiet ways. Apple's Terminal can keep per-session history under ~/.zsh_sessions. Multiplexers and IDE integrated terminals may keep their own scrollback buffers even when the shell history option is tight. Treat "I cleared ~/.zsh_history" as incomplete until you have checked those siblings and any cloud sync folder that mirrors home.
Build a safer default in zshrc or bashrc
A minimal Zsh hardening block:
setopt HIST_IGNORE_SPACE
setopt HIST_REDUCE_BLANKS
# optional: keep history private-ish on a shared admin box
# umask 077 before the shell writes files; know what else it affectsHIST_IGNORE_SPACE is the option that makes a leading space meaningful [2]. Without it, the space is just another character and the secret still lands in the file. Bash users set HISTCONTROL=ignorespace or ignoreboth in ~/.bashrc [4]. Put the setting in the startup file that interactive shells actually read; a forgotten ~/.bash_profile versus ~/.bashrc split is a classic reason the ignore never applies.
For values you need often, prefer a secrets manager CLI that prints to stdout only when you ask, or an env file that your shell sources without echoing. The point is to keep the literal token out of argv history, not to pretend the process environment is secret from your own user.
Incident response when grep hits production
- Rotate the key at the provider. Do not skip this for "it was only on my laptop."
- Search all local history stores (zsh, bash, sessions, Atuin, IDE).
- Search CI logs and chat paste buffers if you ever shared a terminal recording.
- Only then rewrite or truncate history files you control.
- Add ignore-space and
read -shabits so the next token never becomes a line.
If the key was a cloud IAM credential, review access logs for the window since the history line's timestamp. History files often include Unix times in the extended format Zsh uses, which helps bound the window [1].
Patterns worth grepping beyond _KEY=
Expand the pattern when you know your stack: xoxb- (Slack), npm_ tokens, eyJ JWT prefixes you pasted into curl -H, password reset URLs, and BEGIN OPENSSH PRIVATE KEY blocks if someone cat'd a key into a command line. The starter pattern in this article is a beginning, not a compliance scanner. For repository history (a different store), use scan secrets before pushing AI code and the public-repo checklist in make a private repo public.
Browse the Security topic.
Send this to the friend who pastes API keys straight into the terminal with export.

Keep this guide
Filed under Security
Questions about this guide
What does HIST_IGNORE_SPACE do?
In Zsh, any command that starts with a space is not written to the history file when that option is set.
What about Atuin and other history tools?
They keep their own history database, so search that too after you rotate a leaked key.
Is deleting the history line enough?
No. Rotate the key with the provider first. Anything that already read the file may still hold the value.
Does Bash have the same ignore-space idea?
Yes. HISTCONTROL=ignorespace (or ignorespace in a colon list) skips lines that begin with a space.
Sources
- Zsh docs: parameterszsh.sourceforge.io
- Zsh docs: optionszsh.sourceforge.io
- Zsh docs: shell builtin commandszsh.sourceforge.io
- Bash manual: variablesgnu.org