Hero Background

Next-Gen App & Browser Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud
Browsers and OS

7 Commands in Linux to Clear History (With Examples)

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.

Author

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?

  • Single entry: history -d N removes line N from the list.
  • Current session: history -c && history -w clears memory and flushes the file.
  • Permanently: clear the session, then delete ~/.bash_history.
  • Prevent logging: set HISTCONTROL=ignoreboth in ~/.bashrc.

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.

Commands in Linux to Check and Clear Bash 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:

Commands Used in Linux to Clear History for a Specific Entry

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.

  • Open the terminal and execute the following command to display the current command history:
  • history
    

    This will list all previously executed commands with their respective line numbers.

  • Identify the line number of the command you want to remove. For example, if the command you want to remove is on line 15, note down the number.
  • Execute the following command to delete the specific command from the history:
  • history -d 15
    
  • To ensure the history file is updated with the removed command, run the following command:
  • history -w
    

    This will overwrite the .bash_history file with the current history in memory, making sure the changes are saved.

Commands Used in Linux to Clear History for the Current Session

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.

  • Open the terminal and execute the following command to clear the session history stored in memory:
  • history -c
    
  • To ensure the cleared history overwrites the history file, run:
  • history -w
    
  • Close the terminal and reopen it to confirm that the history has been completely cleared.

Commands Used in Linux to Clear History Permanently

If you want to permanently clear all the history (including the .bash_history file), follow these steps:

  • Open the terminal and clear the current session history by executing:
  • history -c
    
  • Save the cleared session history to overwrite the .bash_history file:
  • history -w
    
  • Delete the .bash_history file entirely by running:
  • rm ~/.bash_history
    

Restart the terminal to allow Bash to create a fresh, empty .bash_history file.

Commands Used in Linux to Prevent Bash from Saving History

If you want to prevent Bash from saving commands to the history file temporarily, you can unset the HISTFILE variable:

  • Open the terminal
  • Run the following command to stop Bash from saving commands to the .bash_history file:
  • unset HISTFILE
    

    This removes the link between the session and the history file, so no commands will be saved during the session.

  • To prevent storing commands temporarily during the session, set the history size to zero with this command:
  • export HISTSIZE=0
    

    This ensures no history is retained in memory for the session.

  • To make these changes permanent, edit the .bashrc file in the home directory. Run the following lines at the end of the file:
  • unset HISTFILE  
    export HISTSIZE=0
    
  • Save the file, close the editor, and apply the changes by running the following command:
  • source ~/.bashrc
    

    After applying these changes, Bash will no longer save any command history.

Commands Used in Linux to Automatically Clear History on Logout

To clear the history automatically when logging out, follow these steps:

  • Open your ~/.bash_logout file in a text editor.
  • nano ~/.bash_logout
    
  • Run the following lines to the file:
  • history -c
    history -w
    
  • Save the file and exit the editor.

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.

Test across 3000+ browser and OS environments with TestMu AI

Commands Used in Linux to Filter History with HISTCONTROL

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.

  • ignorespace: Any command prefixed with a leading space is excluded from history. Running export TOKEN=abc123 (note the space before export) keeps it out of the log.
  • ignoredups: Consecutive identical commands are recorded only once, reducing clutter from repeated ls or cd calls.
  • erasedups: Removes all earlier occurrences of a command across the entire history file, not just consecutive ones.
  • ignoreboth: Combines ignorespace and ignoredups in one setting.

Add the following to ~/.bashrc to make the setting permanent:

export HISTCONTROL=ignoreboth
source ~/.bashrc

You can also exclude specific command names entirely using HISTIGNORE. To stop recording ls, cd, and pwd runs:

export HISTIGNORE="ls:cd:pwd"
source ~/.bashrc

How to Securely Delete Bash History with shred

The 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.

  • Clear in-memory history and flush it to the file:
  • history -c && history -w
  • Securely overwrite and remove the history file:
  • shred -u -z ~/.bash_history
  • -u: Truncates and removes the file after overwriting.
  • -z: Adds a final zero-fill pass to hide the overwrite pattern.

On 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.

Conclusion

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

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.

Author

Tahneet Kanwal is a freelance technical content writer with over 2 years of hands-on experience in frontend development and technical writing. She holds a B.Tech in Information Technology from University College of Engineering and Technology (UCET). Tahneet creates clear, SEO-optimized content on web technologies, software testing, and automation tools, leveraging her skills in HTML, CSS, JavaScript, React, Tailwind CSS, and various tools like VS Code, GitHub, Figma, and Canva. She is the author of 30+ technical blogs and an open-source contributor through Hacktoberfest. She has also participated in the Google Cloud Arcade Facilitator Program and holds certifications as a Meta Android Developer (Coursera) and in Web Development (Internshala). Over time, she has evolved her writing to prioritize structure, readability, and SEO while maintaining technical depth.

Open in ChatGPT Icon

Open in ChatGPT

Open in Claude Icon

Open in Claude

Open in Perplexity Icon

Open in Perplexity

Open in Grok Icon

Open in Grok

Open in Gemini AI Icon

Open in Gemini AI

Copied to Clipboard!
...

3000+ Browsers. One Platform.

See exactly how your site performs everywhere.

Try it free
...

Write Tests in Plain English with KaneAI

Create, debug, and evolve tests using natural language.

Try for free

Clear History in Linux FAQs

Did you find this page helpful?

More Related Blogs

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests