Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Learn 7 commands in Linux to clear history: delete single entries, clear sessions, permanently remove .bash_history, or prevent logging with HISTCONTROL. Step-by-step examples.

Tahneet Kanwal
Author
June 15, 2026
Linux Bash stores every command you run in a hidden file called .bash_history, making it easy to recall previous work and reuse complex commands. Understanding how history works is a core skill covered in our Linux interview questions guide.
Clearing that history becomes necessary when a session contains passwords, API tokens, or private paths. Exposed command history is a common risk surface in security testing and penetration testing, making history management a standard security hygiene practice for any Linux user.
This guide covers the commands in Linux to clear history step by step, from removing a single entry with history -d to permanently disabling logging with HISTCONTROL. Each section includes the exact command, what it does, and when to use it.
Overview
What Is Bash History in Linux?
A running log of every command executed in a terminal session, stored in ~/.bash_history. It persists across sessions and is readable with the history command or directly via cat ~/.bash_history.
How Do You Clear Bash History?
When Should You Use shred Instead of rm?
Use shred -u -z ~/.bash_history when the file contains credentials or secrets and forensic recovery is a concern. For routine cleanup, rm is sufficient. Teams that run automated test jobs on TestMu AI's Automation Cloud avoid the problem entirely, since each job starts in a clean, isolated container with no shared history.
Before clearing your history, you can check the commands that have been recorded by using the history command:
$ history
This will display a list of recent commands executed in the terminal, each with a line number.
To view the .bash_history file directly, you can use the cat command:
$ cat ~/.bash_history
There are several ways to clear Bash command history. You can clear specific commands, clear all history for the current session, or completely delete the history file.
Below are the methods:
If you want to clear a specific history, you can use the history -d command. This command is one of the useful commands in Linux to clear history, as it allows you to delete a particular entry from the history list without affecting the rest.
history
This will list all previously executed commands with their respective line numbers.
history -d 15
history -w
This will overwrite the .bash_history file with the current history in memory, making sure the changes are saved.
If you want to clear all the commands from the current session without deleting the .bash_history file, you can use the history -c command. This is one of the useful commands in Linux to clear history, as it clears the current session’s history, but it does not affect the history saved in the .bash_history file.
history -c
history -w
If you want to permanently clear all the history (including the .bash_history file), follow these steps:
history -c
history -w
rm ~/.bash_history
Restart the terminal to allow Bash to create a fresh, empty .bash_history file.
If you want to prevent Bash from saving commands to the history file temporarily, you can unset the HISTFILE variable:
unset HISTFILE
This removes the link between the session and the history file, so no commands will be saved during the session.
export HISTSIZE=0
This ensures no history is retained in memory for the session.
unset HISTFILE
export HISTSIZE=0
source ~/.bashrc
After applying these changes, Bash will no longer save any command history.
To clear the history automatically when logging out, follow these steps:
nano ~/.bash_logout
history -c
history -w
This will ensure that the history is cleared every time you log out. The history -c command clears the in-memory history, and the history -w command writes the cleared history to the .bash_history file.
Rather than clearing history after the fact, the HISTCONTROL variable prevents certain commands from being saved in the first place. This is the preferred approach in automation testing in CI/CD pipeline where credentials or tokens are passed as arguments and must never appear in logs.
Add the following to ~/.bashrc to make the setting permanent:
export HISTCONTROL=ignoreboth
source ~/.bashrcYou can also exclude specific command names entirely using HISTIGNORE. To stop recording ls, cd, and pwd runs:
export HISTIGNORE="ls:cd:pwd"
source ~/.bashrcThe rm command removes the file from the filesystem index, but the raw bytes remain on disk until those blocks are overwritten. On systems handling credentials or sensitive data, use shred to overwrite the file multiple times before deletion.
history -c && history -wshred -u -z ~/.bash_historyOn SSDs, wear-leveling distributes writes across cells, which limits the effectiveness of overwriting. For SSD-based systems, full-disk encryption is the stronger protection against history recovery.
Controlling your Bash command history is a fundamental Linux security practice. Use history -d to remove a single command, history -c to clear the current session, or shred when secure deletion is required. For environments where history should never accumulate, set HISTCONTROL=ignoreboth in ~/.bashrc to prevent the problem before it starts.
For teams running automated test pipelines on Linux, TestMu AI's HyperExecute spins up fresh containerized environments per test job, so shell history never persists between runs by design. Get started with the getting started with HyperExecute to configure clean, isolated execution environments. You may also want to review clear cache in Linux for a broader guide to session cleanup on Linux systems.
Note: This article was researched and drafted with AI assistance, then reviewed, fact-checked, and published by Tahneet Kanwal, Community Contributor at TestMu AI, whose listed expertise includes Software Testing and Technical Writing. Every statistic, link, and product claim was verified against primary sources. Read our editorial process and AI use policy for details.
Did you find this page helpful?
More Related Blogs
TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance