Bash Keeps a log of every command you entered in the terminal. This history file is super useful for recalling previously executed commands. However, there may be instances where you want to clear or remove the history of the current shell session or the entire history of the system. The history command with necessary options is useful for this purpose. This guide will show you how to clear history in Bash using easy and simple scripts.
- Usage of various options with the history command.
- Clearing the history of the current shell session.
- Clearing history saved in the history file.
- Limiting the size of the history file.
2 Cases of Clearing History Using Bash Script
The history of the current shell session is not immediately saved in the history file. Hence, it’s crucial to know whether one wishes to exclude the history of the current session from being saved in the history file, or if they want to remove both the history of all previous sessions and the current session from the history file. The following cases explain both of these.
Case 1: How to Clear Current Shell Session History in Bash
The current shell session refers to the currently opened Terminal session. Each time you open a new terminal window you start interacting with a new shell session. The history command with -c option is used to clear the history of the current shell session.
❶ At first, launch an Ubuntu Terminal.
❷ Write the following command to open a file named history_cs.sh in the build-in nano editor:
❸ Copy the following scripts and paste them into nano. Press CTRL+O and ENTER to save the file; CTRL+X to exit. Alternatively, copy the following script. Paste the script in a text editor and save it as .sh file.
Script (history_cs.sh) >
#!/bin/bash # Display the history before clearing echo "History before clearing:" history # Write the current history to the .bash_history file history -w # Clear the history of the current shell session history -c # Display the history after clearing echo "History after clearing:" history
❹ Use the following two commands to make both file executable:
❺ Run the history_cs.sh script by the following command:
chmod u+x history_cs.sh
The output first show the history of commands executed in the current shell. Then it clears the history using history -c. After that, it tries to display history again. However, it finds an empty list as previously executed commands of the current shell are already cleared.
To clear history of bash_logout file in each user log out run following command:
cat /dev/null > ~/.bash_logout
Case 2: How to Clear Entire History File in Bash
Usually, executed commands are written in the ~/.bash_history file. Even if the user removes the history of the current shell, there may be a history of previous shell sessions written in the history file. See the following script to see how to clear the entire history file.
Script (history_ps.sh) >
#!/bin/bash # See the history file cat ~/.bash_history # Clear the history file truncate -s 0 ~/.bash_history
Run the history_cs.sh script by the following command:
Upon execution, the script shows the history file. After that, it clears the history file and makes its length zero.
- Apart from clearing the history file using Bash script, users can clear the file from the command line using the following command.
cp /dev/null ~/.bash_history
- If you want to delete the whole history file instead of just clearing the file content, run the following command.
- Moreover, there is a way to disable the history file permanently. Run the following command to permanently disable it.
ln -sf /dev/null ~/.bash_history
How to Limit the Size of the History File in Bash
History files typically store the last 1000 executed commands. However one can change the size of the history file.
Script (histsize.sh) >
#!/bin/bash # Current size of the history file echo $HISTSIZE # Set new size HISTSIZE=20 # New size of the history file echo $HISTSIZE
Run the histsize.sh script by the following command:
The images show that the size of the history file was 1000. But the program changes the file size to 20. So the history file now can store only the last 20 commands.
Clearing History vs clear Command
The clear command and clearing history are not the same. The table below contains basic differences between these.
|Clearing history||Clear command|
In conclusion, the user has several options when it comes to clearing history in Bash. A user can clear a single line, a range of lines or entire history files. Even a user can change the size of the history file. I believe you can perform all these using various options of the history command discussed in this article.
People Also Ask
- How to Get Date in Bash [2 Methods with Examples]
- How to Print Time in Bash [2 Quick Methods]
- How to List Users in Bash [2 Easy Ways]
- How to Get Current Time in Bash [4 Practical Cases]
- How to Use Date Format in Bash [5 Examples]
- How to Get Timestamp in Bash [2 Practical Cases]
- How to Copy and Paste in Bash [2 Methods & Cases]
- How to Read Password in Bash [3 Practical Cases]
- How to Send Email in Bash [2 Easy Methods]
- Bash Script to Send Email with Attachment [Step-by-Step Guide]
- How to Get IP Address in Bash [3 Methods]
- How to Find and Replace String in Bash [5 Methods]
- How to Get Script Name Using Bash Script? [3 Easy Ways]
- How to Call Another Script in Bash [2 Methods]
- How to Generate UUID in Bash [3 Simple Methods]
- 3 Easy Ways to Write to a File in Bash Script
- How to Write the Output to a File in Bash Script [5 Practical Cases]
- How to Create a List in Bash Scripts? [2 Easy Methods]
- How to Clear Screen Using Bash Script? [2 Effective Methods]
- How to Check Ubuntu Version Using Bash Scripts? [2 Methods]