Written byJenny Zeng
How to Clear Command History in Mac Terminal?
The Terminal app on Mac allows you to interact with the operating system using command lines in a shell environment like Zsh or Bash. Such a shell environment will save your command history for future retrieval, enabling you to cycle through previous commands by pressing the up and down arrow keys.
While it's useful to reuse the same commands without typing, it can also compromise your privacy, as anyone with access to your account can easily view your command history. Besides, you might want to clear the command history in a session to start fresh.
In this article, we'll explain how to clear Terminal history on Mac both permanently and temporarily.
Table of Contents:
- 1. How to check Terminal history on Mac?
- 2. How to clear Terminal history on Mac?
- 3. FAQs about how to clear Terminal history on Mac
How to check Terminal history on Mac?
Typically, you can use one of the following two ways to check Terminal history on Mac:
① Check recent Terminal history by running the history command.
- Launch Terminal.
- Type the command below and hit Enter.history
- View the most recent 16 commands you've executed in Terminal.
② Show all Terminal history by opening the history file.
- Open Terminal.
- Type the command suitable for your shell environment and hit Enter.cat ~/.zsh_historycat ~/.bash_history
✳︎ Zsh (%) is the default shell on macOS 10.15 Catalina and later; Bash ($) is the default shell on macOS 10.14 Mojave and earlier.
- Check a list of all the commands previously used in Terminal.
Share the steps to assist others in checking the command history in Terminal.
How to clear Terminal history on Mac?
There are two ways to clear Terminal history on Mac for Zsh or Bash. You can apply them to clear Terminal history on any Mac OS X or macOS version, including macOS Monterey, Big Sur, Catalina, etc.
Clear current session's command history using the historyommand
If you want to remove the list of commands that you have typed in the current Terminal session, making them no longer visible or accessible through the history navigation features (like the up and down arrow keys), you can use the history command.
This won't affect the commands stored in the persistent history file, which maintains a record of commands from past sessions.
For Zsh users:
For Bash or other shell users:
Note: If you run "history -c" in a Zsh shell, you'll receive the error that says "history: bad option: -c."
Clear command history in Mac Terminal by deleting the history file
If you'd like to clear the entire Terminal command history, you can delete the hidden history file, which is located at ~/.zsh_history or ~/.bash_history. This file will be regenerated when you run new commands after reopening the Terminal app.
Remember that this clears all Terminal history forever, so make sure it's what you want to do before running the command.
How to clear Terminal history forever on Mac (Zsh/Bash):
- If you have multiple Terminal sessions, close the rest and leave one open.
- Clear the history of the current session.
For Zsh users: history -pFor Bash users:history -c
- Remove the history file to delete the command history permanently.
For Zsh users:rm ~/.zsh_history
For Bash users:rm ~/.bash_history
- Close and reopen Terminal.
After taking the above steps, all the command history should have been cleaned, except the rm command itself and the commands that weren't written to the file when the deleting process was initiated.
Share this post if you find it helpful!
FAQs about how to clear Terminal history on Mac
You can delete previous words in Mac Terminal by using the left or right arrow to navigate to the word and pressing the Delete key.
Yes, the Terminal app on Mac has a history file.
To abort a command in Terminal, use Contrl + U.