# Your shell history may contain API keys

> export API_KEY=... typed last month can still be in that plain text file.

Source: https://fullbytedeveloper.com/blog/hist-key
Published here: 2026-11-19, updated 2026-12-03

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].

```bash
grep -nE 'sk-|ghp_|AKIA|_KEY=' ~/.zsh_history
```

On this host a throwaway zsh-format file with a fake key printed:

```bash
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_history
```

Captured output:

```text
2:: 1700000001:0;export API_KEY=fake123sk-test
```

Using Apple's Terminal? It also keeps per-session copies under `~/.zsh_sessions`. Search those too:

```bash
grep -rnE 'sk-|ghp_|AKIA|_KEY=' ~/.zsh_sessions
```

## After 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`:

```bash
setopt HIST_IGNORE_SPACE
```

Then start any secret command with a leading space. Bash uses `HISTCONTROL=ignorespace` for the same idea [4].

```bash
# 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):

```text
ls$
export VISIBLE=plain$
history -w$
```

## Prefer prompting over exporting

`read -s` keeps the value out of the typed command line [3][4]:

```bash
read -s API_KEY && export API_KEY
```

The 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.

<figure>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 720 260" role="img" aria-label="Typed export lands in history; space prefix or read -s keeps the secret out">
  <defs>
    <marker id="histArrow" markerWidth="10" markerHeight="10" refX="8" refY="3" orient="auto">
      <path d="M0,0 L8,3 L0,6 Z" fill="#14b8a6"/>
    </marker>
  </defs>
  <rect x="0" y="0" width="720" height="260" rx="12" fill="#0b1f3a"/>
  <text x="24" y="36" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="16" font-weight="700">export API_KEY=...</text>
  <path d="M220 30 H268" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#histArrow)"/>
  <rect x="276" y="12" width="200" height="48" rx="8" fill="#123052" stroke="#f59e0b" stroke-width="2"/>
  <text x="288" y="42" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">~/.zsh_history</text>
  <path d="M476 36 H524" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#histArrow)"/>
  <rect x="532" y="12" width="160" height="48" rx="8" fill="#123052" stroke="#64748b" stroke-width="2"/>
  <text x="544" y="42" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">readable as you</text>
  <text x="24" y="110" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="16" font-weight="700">Safer paths</text>
  <rect x="24" y="128" width="300" height="96" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="36" y="160" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">1. leading space + HIST_IGNORE_SPACE</text>
  <text x="36" y="186" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">2. read -s then export</text>
  <text x="36" y="212" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">3. rotate if grep already hit</text>
  <path d="M324 176 H372" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#histArrow)"/>
  <rect x="380" y="140" width="300" height="72" rx="8" fill="#123052" stroke="#f59e0b" stroke-width="2"/>
  <text x="392" y="172" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">Secret never becomes a</text>
  <text x="392" y="196" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">history line (rotate old ones)</text>
</svg>
</figure>

*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.

## Related checks

Shell history is one leak path. Env files and agent access are others: [public env prefixes](/blog/public-env), [scan secrets before pushing AI code](/blog/scan-secrets-before-pushing-ai-code), and [Claude Code env lock](/blog/claude-code-env-lock). For everyday terminal navigation without dumping secrets into scrollback, see [essential Linux commands part 1](/blog/essential-linux-commands-developers-part-1).

## 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:

```bash
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 affects
```

`HIST_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

1. Rotate the key at the provider. Do not skip this for "it was only on my laptop."
2. Search all local history stores (zsh, bash, sessions, Atuin, IDE).
3. Search CI logs and chat paste buffers if you ever shared a terminal recording.
4. Only then rewrite or truncate history files you control.
5. Add ignore-space and `read -s` habits 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].

## Shared machines and containers

On a jump host with multiple engineers, history files under each home still matter, and root can read them. In containers, people sometimes `docker exec` into a running app container and export a debug token. That line can persist in the container user's history layer if the image or volume keeps `$HOME`. Wipe teaching containers after demos. Do not bake `HISTFILE` into a shared volume used by more than one person.

## 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](/blog/scan-secrets-before-pushing-ai-code) and the public-repo checklist in [make a private repo public](/blog/make-private-repo-public-checklist).

Browse the [Security topic](https://fullbytedeveloper.com/blog/topic/security).

Send this to the friend who pastes API keys straight into the terminal with export.

Image: Dark grid floor with a translucent blue conveyor-like plane holding five raised gray slots. The rightmost slot carries a glowing teal-and-amber rounded square that lights the surface. Teal and amber arrows, orbital rings, small cubes, pills, and spheres surround the plane.
https://fullbytedeveloper.com/media/hist-key/hist-key-venice.webp
One glowing slot on a long strip is enough: a secret export can sit in shell history until you rotate it.

## Sources

1. [Zsh docs: parameters](https://zsh.sourceforge.io/Doc/Release/Parameters.html)
2. [Zsh docs: options](https://zsh.sourceforge.io/Doc/Release/Options.html)
3. [Zsh docs: shell builtin commands](https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html)
4. [Bash manual: variables](https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html)
