Home > News Tips

How to Clear Command History in Mac Terminal?

Updated on Tuesday, December 3, 2024

iBoysoft author Jenny Zeng

Written by

Jenny Zeng
Professional tech editor

Approved by

Jessica Shee

English Français Deutsch やまと Español Português

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.

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.

  1. Launch Terminal.
  2. Type the command below and hit Enter.historyHow to check Terminal history on Mac
  3. View the most recent 16 commands you've executed in Terminal.

Show all Terminal history by opening the history file.

  1. Open Terminal.
  2. Type the command suitable for your shell environment and hit Enter.cat ~/.zsh_historycat ~/.bash_historyHow to show all Terminal history on Mac
    ✳︎ Zsh (%) is the default shell on macOS 10.15 Catalina and later; Bash ($) is the default shell on macOS 10.14 Mojave and earlier.
  3. 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 history command

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:

history -p

How to clear the Terminal history of the current session on Mac

For Bash or other shell users: 

history -c

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

  1. If you have multiple Terminal sessions, close the rest and leave one open.
  2. Clear the history of the current session.
    For Zsh users: history -pFor Bash users:history -c
  3. Remove the history file to delete the command history permanently. 
    For Zsh users:rm ~/.zsh_historyHow to clear Terminal history on Mac forever
    For Bash users:rm ~/.bash_history
  4. 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!