TL;DR
To clear Bash history, you can try these three methods:
- For real-time clearing Bash history, use the
history -ccommand in the Terminal app. - To permanently clear Bash history, run
echo '' > ~/.bash_historyto clear the .bash_history file. Then, addunset HISTFILEto the ~/.bashrc file to stop future history saving. - To completely disable Bash history, execute
set +o history, followed byhistory -cto 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:
- First, view the Bash history by executing the
historycommand in the Terminal app.

- Now, enter the command
history -cin the Terminal app. This command clears the history buffer and removes all the commands from it.
- The next time you enter a command, it will be the first command in your Bash history. To verify this, run the
historycommand.
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
- Head to the home directory and search for the .bash_history file.
- Once found, click the file to open and view the Bash history.
- Now, head to the Linux command prompt and execute the
echo "" > ~/.bash_historycommand.
- 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.
- To prevent Bash from saving history in the future, run the
nano ~/.bashrcin the Terminal app.
- At the end of the file, add a new line at the end of the file and type
unset HISTFILEon the new line.
- Then, press Ctrl + O to save and Ctrl + X to exit the nano editor.
- Type
source ~/.bashrcand press Enter to reload the ~/.bashrc file. In some cases, you may need to restart the command prompt to apply the changes.
- Now, the Bash history will not store any future commands in the history file. However, you may need to run the
history -ccommand to remove all previously saved or stored history.
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:
- Execute the
historycommand to view the saved history.
- Then, in the Terminal app, run the command
set +o historyin the command prompt to disable the Bash history for the current session.
- 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 --versioncommand in Bach’s history.
- But if you want to enable Bash history again, enter the
set -o historycommand.
- Now, any commands you enter will be added to the Bash history list. As you may see, the commands executed after
set -o historyare saved in the 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 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 " 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: