How to Clear Bash History in Linux [3 Simple Ways]

Written by

Reviewed by

Last updated: July 24, 2024

Expert verified

SVG Image

TL;DR

To clear Bash history, you can try these three methods:

  1. For real-time clearing Bash history, use the history -c command in the Terminal app.
  2. To permanently clear Bash history, run echo '' > ~/.bash_history to clear the .bash_history file. Then, add unset HISTFILE to the ~/.bashrc file to stop future history saving.
  3. To completely disable Bash history, execute set +o history, followed by history -c to erase previously saved history.

Clear Bash history effortlessly with the concise guide below. Along with that, you’ll also learn the three best practices and five quick tips for managing the Bash history.

Wondering if someone could see your command history in Linux? It’s a common concern, but clearing your Bash history can safeguard your privacy and security. In this post, I’ll show you how to clear Bash history in real time, delete it permanently, and even disable it. You’ll also find best practices and quick tips to manage your Bash history more effectively. This guide will definitely enhance your Bash skills.

What is Bash History?

Bash history is a feature in the Bash shell, a common command-line interface in Linux. When you type commands into the terminal, Bash saves them in a file called .bash_history. This file keeps a record of the commands you’ve entered, so you can easily access and reuse them later.

For example, you can press the up arrow key to scroll through your previous commands and run them again without retyping. This helps speed up repetitive tasks and makes command-line work more efficient.

Why Clear Bash History?

Clearing Bash history is important for several reasons:

  • Avoid Mistakes: Old commands in your history can sometimes cause confusion or errors if accidentally reused. Clearing history reduces the risk of running outdated or incorrect commands.
  • Privacy: If someone else uses your computer or gains access to your user account, they can see your command history. This could reveal sensitive information like file paths, server addresses, or passwords.
  • Security: Malicious users can exploit your command history to find vulnerabilities in your system. For example, they might find commands that show how to access secure areas of your network.
  • Clean Up: Over time, your Bash history file can become cluttered with old and irrelevant commands. Clearing it helps keep things organized and makes it easier to find recent commands.

How to Clear Bash History in 3 Simple Ways

To clear Bash history in Linux, start by using the history -c command to clear the current session’s history. Next, permanently delete the history by running rm ~/.bash_history and then echo "" > ~/.bash_history. Finally, disable Bash history logging by adding unset HISTFILE to your ~/.bashrc file and sourcing it with source ~/.bashrc.

For a better understanding, let’s have a look at each of these methods here:

1. Clear Bash History in Real-Time

Use this method to clear Bash history in real-time when you want to ensure that your command history is immediately removed as you execute commands. This can be beneficial in situations where you want to maintain privacy, prevent sensitive information from being stored, or start with a clean history for subsequent commands. Here are the steps to follow:

  1. First, view the Bash history by executing the history command in the Terminal app.
view bash history by executing command
  1. Now, enter the command history -c in the Terminal app. This command clears the history buffer and removes all the commands from it.
command clears the history buffer
  1. The next time you enter a command, it will be the first command in your Bash history. To verify this, run the history command.
to verify this run clear bash history

2. Clear Bash History File Permanently

If you want to completely clear Bash history, you can do so by manually clearing the .bash_history file. This file keeps a record of all the commands you’ve entered. Just follow these steps to clear the Bash history file manually

  1. Head to the home directory and search for the .bash_history file.
head to home directory and search
  1. Once found, click the file to open and view the Bash history.
click file to open and view bash history
  1. Now, head to the Linux command prompt and execute the echo "" > ~/.bash_history command.
head to linux command prompt and execute
  1. This command overwrites the .bash_history file with a blank line, effectively deleting all the commands stored in it. You can check it by viewing the .bash_history file.
command overwrites bash history file
  1. To prevent Bash from saving history in the future, run the nano ~/.bashrc in the Terminal app.
prevent bash from saving history in future
  1. At the end of the file, add a new line at the end of the file and type unset HISTFILE on the new line.
add a new line at end of the file
  1. Then, press Ctrl + O to save and Ctrl + X to exit the nano editor.
exit the nano editor
  1. Type source ~/.bashrc and press Enter to reload the ~/.bashrc file. In some cases, you may need to restart the command prompt to apply the changes.
you need to restart command prompt to apply changes
  1. Now, the Bash history will not store any future commands in the history file. However, you may need to run the history -c command to remove all previously saved or stored history.
bash history will not store any future commands

3. Disable Bash History

Another way is to disable Bash history to prevent command recording and maintain privacy. To do this, you can use the set +o history command. Here’s is the step-by-step guide to use this method:

  1. Execute the history command to view the saved history.
execute history command to view saved history
  1. Then, in the Terminal app, run the command set +o history in the command prompt to disable the Bash history for the current session.
run command to disable bash history
  1. Now, any commands you enter will not be added to the history list. Just like in the following output, it did not save the python3 --version command in Bach’s history.
any commands you enter will not be added
  1. But if you want to enable Bash history again, enter the set -o history command.
if you want to enable bash history again
  1. Now, any commands you enter will be added to the Bash history list. As you may see, the commands executed after set -o history are saved in the Bash history.
any commands you enter will be added to bash history

Entirely disabling the Bash history may affect your productivity, as you will not be able to recall previous commands. So, it is recommended to clear your Bash history regularly instead of disabling it completely.

3 Best Practices To Manage Bash History 

Managing your Bash history is important for maintaining the security, efficiency, and organization of your command line activities. Here are some best practices to consider:

1. Setting Bash History Limits

By default, Bash history stores up to 500 commands, allowing you to access and repeat previously executed commands easily. However, if you find that you require a larger or smaller command history size, you have the flexibility to adjust this limit according to your needs. To do so, add the following lines to your ~/.bashrc file:

HISTSIZE=1000 
HISTFILESIZE=2000
bash history stores up to 500 commands

These lines set the maximum number of commands that Bash history can store to 1000 in memory and 2000 in the .bash_history file. This limit prevents your history file from becoming too large, slowing down your Terminal’s performance.

2. Use Customization to Clear Bash History

By default, Bash history only shows the commands you entered without timestamps, but when you add HISTTIMEFORMAT="%d/%m/%y %T ", it will display the timestamp of when each command was executed in the format of day/month/year hour:minute:second.

While this line does not directly help with clearing Bash history, it can provide useful information for tracking and analyzing your command history. To customize your Bash history, add the following lines to your ~/.bashrc file:

HISTTIMEFORMAT="%d/%m/%y %T "
customise your bash history add following lines

Save the file after making the changes, and the new timestamp format will take effect the next time you open a Bash shell. Basically, this line will change the timestamp format of your Bash history to the date and time format (%d/%m/%y %T) that will help you to review your command history with timestamps displayed according to your chosen format.

Feel free to experiment with different format specifiers, such as %Y for the four-digit year, %b for the abbreviated month name, or %I:%M:%S %p for a 12-hour clock format with AM/PM indication. You can customize the timestamp format to your liking, making it more meaningful and easier to interpret for your command history analysis or personal reference.

3. Automate Clear Bash History Process

To automate the process of clearing your Bash history, you can create a cron job that runs every day at midnight and overwrites the .bash_history file with a blank line. To set up the cron job, open your terminal and type crontab -e to edit your user’s crontab file. Add the following line to the file, save and close it:

0 0 * * * echo "" > ~/.bash_history
set up cron job open your terminal

The cron job will now run every day at midnight and clear Bash history. However, it is important to regularly review your Bash history for any suspicious or unauthorized activity and investigate any potential security breaches or incidents.

Pros and Cons of Clearing Bash History

ProsCons
– Enhances security– Loses command history for troubleshooting
– Protects privacy– Inconvenient for reusing previous commands
– Reduces risk of exploitation– Extra effort to remember or document commands
– Keeps history file clean– Might delete useful command references

5 Quick Tips to Clear Bash History More Efficiently

With the help of these five handy tips and commands, you can effortlessly clear your Bash history, safeguarding your sensitive information and ensuring your privacy. Let’s have an in-depth look into each of them here:

  • 🔍 Use the history -d command to delete specific entries: The history -d command allows you to delete specific entries from your history buffer. For example, history -d 100 will delete the 100th entry in your history buffer.
  • 🙈 Use HISTIGNORE to exclude specific commands from history: You can use the HISTIGNORE environment variable to exclude specific commands from being added to the history buffer. For example, HISTIGNORE="ls:cd" will exclude the ls and cd commands from being added to the history buffer.
  • 🔧 Use HISTCONTROL to control how Bash saves history: You can use the HISTCONTROL environment variable to control how Bash saves history. For example, HISTCONTROL=ignorespace will prevent any commands that start with a space from being added to the history buffer.
  • 🗑️ Clear history for a specific session with the kill 9: The kill−9 command kills the current Bash session, clearing the history buffer for that session only. This can be useful if you want to clear the history for a specific session without affecting the overall history file.
  • 🔒 Encrypt your Bash history file for added security: If you want to keep a record of your commands but also want to protect them from prying eyes, you can encrypt your Bash history file using tools like gpg or OpenSSL. This ensures that even if someone gains access to your history file, they will not be able to read the contents without the encryption key.

Key Takeaways

In this guide, you’ve learned the top three methods to clear Bash history in Linux: real-time clearing, permanent deletion, and disabling Bash history. I also covered best practices like setting history limits, customizing history with timestamps, and automating the clearing process.

If you want to explore more into Bash scripting, I suggest exploring:

Frequently Asked Questions

What is Bash history?

Bash history is a log file that stores a record of all commands entered in the command-line interface. By default, it is stored in the ~/.bash_history file in the user’s home directory, listing commands in reverse chronological order with corresponding numbers.
This feature is particularly useful for recalling previously executed commands, especially those that were complex or that need to be repeated.

How does Bash history work?

Bash history works by storing commands in a buffer in memory, which is then flushed to a history file when the Terminal is closed. You can navigate their command history using shortcuts and commands, search through it using the history command, and even customize it by creating aliases and functions. Bash history options can also be modified to suit individual preferences. However, it is important to note that the feature may not be enabled by default in all operating systems or Terminal emulators, requiring manual configuration.

Why is it important to clear the Bash history?

Clearing Bash history is important for security, privacy, legal, and performance reasons. Sharing a computer can allow others to view sensitive information about Bash’s history, and it may reveal personal information. Clearing the history regularly can help protect your privacy and prevent sensitive information from being used against you in legal proceedings. Additionally, a large history file can slow down terminal performance.

Does clearing Bash history affect my command-line productivity?

Regularly clearing your Bash history can prevent others from seeing your sensitive commands, such as passwords or API keys, if they gain access to your computer or account. It also removes clutter from your history, making it easier to find the commands you need. However, if you frequently use the command history to save time and increase efficiency, clearing it may slow you down as you have to type commands from scratch. In such cases, consider adjusting your history settings to limit the number of commands saved or using alternative tools such as shell aliases or functions to save frequently used commands.

How to view Bash history in Linux?

To view the Bash history in Linux, open the Terminal and enter the history command. This command will display a list of all the previously executed commands, along with their corresponding command numbers. You can use these numbers to execute a specific command from the history list by entering !commandnumber in the Terminal. For example, if you want to re-run command number 10, execute !10 in the Terminal.

What are the common commands used in Bash History?

Commonly used commands include history to show the entire command history, ! followed by a number to re-run a specific command, Ctrl + R to search for a command, and !! to re-run the last command in history.
Additionally, you can use the up and down arrow keys to navigate through their command history, use the !!:p command to print the last command without executing it, and use the history -c command to clear their command history. By mastering these commands, users can become more efficient and effective in their use of the Bash shell.

How to recover bash history? Or is there a way for backup?

To recover lost or deleted Bash history or create a backup, you can follow these steps. First, check the location of your Bash history file, usually named .bash_history, in your home directory. If the file is accidentally deleted or modified, attempt file recovery using backups or file recovery tools like extundelete. If you have a backup of the Bash history file, restore it to its original location. Additionally, explore system backups or version control systems for previous versions of the file.

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

How to Check Log4j Version in Linux [3 Best Ways]

Next Post

4 Easy Ways to Count Files in Linux Using CLI

Leave a Reply

Your email address will not be published. Required fields are marked *

Read next