# history

> The `history` command in Unix-like systems lists previously entered commands, allowing users to review or reuse them.

## How do I see and rerun previous commands with history?

The command that displays previously entered commands. Useful for reviewing past actions and quickly reusing commands.

- Topic: Terminal
- Type: command

## Example

Wrong:

```bash
curl -H "Authorization: Bearer abc123secret" https://api.example.com/me
```

Right:

```bash
read -rs TOKEN
curl -H "Authorization: Bearer $TOKEN" https://api.example.com/me
```

`read -s` takes the token without echoing it, so the secret never lands in shell history.

## Common mistakes

- Typing a password or token in a command and leaving it in the history file.

## Key points

- Secrets are read from env files or prompts, not typed inline, since commands stay in history.
- `!!` and `!n` are not used in scripts, as history expansion is off in non-interactive shells.

## Interview questions

### How do I search my command history?

Press `Ctrl+R` and type part of a command. Press `Ctrl+R` again to step to older matches.

### How do I remove a command from history?

In bash, run `history -d <number>` and then `history -w`. Or edit `~/.bash_history` or `~/.zsh_history` directly.

### Where is shell history stored?

Usually in `~/.bash_history` for bash and `~/.zsh_history` for zsh, written when the shell exits.

## Related cards

- [clear](https://wonderium.dev/processes/clear.md)
- [`grep "<Word>" <file>`](https://wonderium.dev/processes/grep-word.md)
- [.env](https://wonderium.dev/processes/env.md)
- [Terminal](https://wonderium.dev/engineering/terminal.md)
- [Sensitive Data Leakage](https://wonderium.dev/ai/sensitive-data-leakage.md)

---

- Level: [Processes](https://wonderium.dev/processes.md)
- HTML version: https://wonderium.dev/processes/history
- Last updated: 2026-11-25
