Note : Cet article a été publié à l’origine en 2012 and has been updated to cover both Bash and Zsh, since macOS Catalina (10.15) changed the default shell from Bash to Zsh. Consultez la documentation actuelle de Apple pour les informations les plus récentes.
Your Mac terminal keeps a record of every command you type. This history feature is useful for recalling previous commands, but there are times when you need to clear it — whether you accidentally typed a password in plain text, want to remove sensitive server addresses, or simply want a fresh start. This guide covers every method for clearing terminal history on macOS, explains how the history system works under the hood, and shows you how to configure it for privacy.
How Terminal History Works on macOS
When you type commands into Terminal (or any terminal emulator like iTerm2), the shell records them in two places:
- In-memory history — the list of commands from the current session, stored in RAM.
- Persistent history file — a file on disk that preserves your command history between sessions.
When you open a new terminal window, the shell reads the history file and loads previous commands into memory. As you type new commands, they are added to the in-memory list. When the session ends (you close the terminal), the shell writes the in-memory history to the persistent file.
History File Locations
The location depends on which shell you are using:
| Shell | History File | Configuration File |
|---|---|---|
| Zsh (default since macOS Catalina) | ~/.zsh_history | ~/.zshrc |
| Bash (default before Catalina) | ~/.bash_history | ~/.bashrc or ~/.bash_profile |
To check which shell you are currently using:
echo $SHELL
This will output something like /bin/zsh or /bin/bash.
Method 1: Clear the Current Session History
The quickest way to clear the history for your current terminal session is the history -c command:
history -c
This removes all commands from the in-memory history list for the active session. After running this command, pressing the up arrow key will no longer cycle through previous commands.
Important : This only clears the in-memory history. The persistent history file on disk is not affected. If you open a new terminal window, the old history will still load from the file.
Method 2: Delete the Persistent History File
To completely remove all stored history, delete the history file itself.
For Zsh users:
rm ~/.zsh_history
For Bash users:
rm ~/.bash_history
After deleting the file, clear the in-memory history as well:
history -c
The shell will create a new, empty history file automatically when you start the next session.
Method 3: Truncate the History File Without Deleting It
If you prefer not to delete the file (for example, because other scripts or tools expect it to exist), you can truncate it to zero bytes:
# For Zsh
> ~/.zsh_history
# For Bash
> ~/.bash_history
The > redirect operator with no input overwrites the file with empty content. Then clear the in-memory history:
history -c
Method 4: Clear History and Prevent It from Being Rewritten
When you run history -c, the shell may still write some history back to the file when the session ends. To ensure a clean slate, combine all steps:
# Clear in-memory history
history -c
# Truncate the history file
> ~/.zsh_history # or > ~/.bash_history for Bash
# Start a new shell session (optional but recommended)
exec $SHELL -l
The exec $SHELL -l command replaces the current shell process with a fresh login shell, ensuring no residual history data remains in memory.
Method 5: Remove Specific Commands from History
Sometimes you do not want to clear the entire history — just a few sensitive entries. In Zsh and Bash, you can delete a specific entry by its line number.
First, find the line number:
history | grep "sensitive-command"
Then delete that specific entry. In Bash:
history -d 142
In Zsh, the history -d flag is not available. Instead, you need to edit the history file directly:
# Open the history file in a text editor
nano ~/.zsh_history
Search for the offending line, delete it, save the file, and then reload the history:
fc -R
Configuring History Behavior
Controlling History Size
You can control how many commands are stored by setting environment variables in your shell configuration file.
For Zsh (~/.zshrc):
# Number of commands kept in memory during a session
HISTSIZE=1000
# Number of commands saved to the history file
SAVEHIST=1000
# Location of the history file
HISTFILE=~/.zsh_history
For Bash (~/.bashrc or ~/.bash_profile):
# Number of commands kept in memory during a session
HISTSIZE=1000
# Number of commands saved to the history file
HISTFILESIZE=1000
# Location of the history file
HISTFILE=~/.bash_history
Disabling History Entirely
To completely disable command history recording, set the size to zero:
# Add to ~/.zshrc or ~/.bashrc
HISTSIZE=0
SAVEHIST=0
After adding these lines, open a new terminal session. No commands will be recorded. Note that this disables a very useful productivity feature, so consider the other approaches below before taking this step.
Preventing Specific Commands from Being Recorded
In Zsh, enable the HIST_IGNORE_SPACE option to skip any command that starts with a space:
# Add to ~/.zshrc
setopt HIST_IGNORE_SPACE
Then, prefix any sensitive command with a space:
mysql -u root -pMySecretPassword # note the leading space
This command will not appear in history.
In Bash, use the HISTCONTROL variable:
# Add to ~/.bashrc
HISTCONTROL=ignorespace
The behavior is the same: prefix the command with a space and it will not be recorded.
Removing Duplicate Entries
To keep your history clean and avoid storing the same command multiple times:
For Zsh (~/.zshrc):
setopt HIST_IGNORE_ALL_DUPS # Remove older duplicates when a new duplicate is added
setopt HIST_FIND_NO_DUPS # Do not display duplicates when searching history
For Bash (~/.bashrc):
HISTCONTROL=ignoredups:erasedups
Sharing History Across Terminal Sessions (Zsh)
By default, each terminal window in Zsh maintains its own separate history. To share history across all open terminal sessions in real time:
# Add to ~/.zshrc
setopt SHARE_HISTORY
With this enabled, a command typed in one terminal window will immediately appear in the history of another window.
Privacy Considerations
Terminal history can contain sensitive information including:
- Passwords passed as command-line arguments (e.g.,
mysql -u root -pPassword123) - API keys and tokens used in
curlcommands or environment variable exports - Internal server addresses and infrastructure details
- Database connection strings with credentials embedded
If security is a concern in your environment, consider these best practices:
- Use environment variables or configuration files for credentials instead of passing them as command-line arguments.
- Enable HIST_IGNORE_SPACE and prefix sensitive commands with a space.
- Periodically review and clear your history file.
- Set a reasonable HISTSIZE so the history does not grow indefinitely.
- Encrypt your home directory using FileVault, which protects the history file at rest.
Quick Reference
| Task | Command |
|---|---|
| Clear current session history | history -c |
| Delete Zsh history file | rm ~/.zsh_history |
| Delete Bash history file | rm ~/.bash_history |
| Truncate history file | > ~/.zsh_history |
| Check current shell | echo $SHELL |
| View full history | history |
| Search history | history | grep "keyword" |
| Reload history from file | fc -R (Zsh) |
| Start fresh shell session | exec $SHELL -l |