How to Clear Your Shell History

'Shell' in bold white text on a black rectangle with colorful polygon shapes around it.

In this post, I'll guide you through the process of clearing your shell history.

While maintaining your shell history can be convenient for suggesting commands and minimizing errors, having a couple thousand entries in the history every so often gets a bit messy. Also if you are a hacker or are concerned about privacy it could be wise to delete your history every once in a while, or every day even.

Step 1: Clear Shell History

history -c

After executing the command, reopen the shell. Then, run the history command to verify that your history has been cleared. You might notice one line remaining, which typically says "history."

If the above method doesn't work on your system, proceed with a manual override of the history file.

Step 2: Determine History File Location

To determine the correct file associated with the $HISTORY variable, use the following command:

echo $HISTORY

For instance, the output might be /Users/timothy/.zsh_history, indicating the file to override.

Step 3: Manually Override the History File:

To manually clear the history file, execute the appropriate command for your shell. For instance, in zsh, you can use:

cat /dev/null > ~/.zsh_history

This command writes nothing (represented by /dev/null) to the specified history file, effectively clearing its contents.

Congratulations! You have successfully cleared your shell history, promoting privacy and organization. Now, get back to using that sharp mind!

Fun fact: /dev/null is humorously referred to as the "bit bucket" or "black hole" of the unix operating system. It's commonly used by hackers to redirect output, avoiding detection and leaving no evidence in log files.