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.
Key Takeaways
- 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.
Free Downloads
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:
nano history_cs.sh
- nano: Opens a file in the Nano text editor.
- history_cs.sh: Name of the 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
The provided Bash script serves to clear the history of the current shell session. The script begins by displaying the history of the current session using the history command. Next, it uses history -w to write the current history to the history file, typically located at ~/.bash_history. This ensures that any recent commands executed in the session are saved to the history file before proceeding.
Then, the script executes history -c, which clears the commands from the memory of the current session without affecting the history file. The final step is to display history again using the history command, which should now indicate an empty history as the session history has been successfully cleared.
❹ Use the following two commands to make both file executable:
chmod u+x history_cs.sh
- chmod: Changes permissions.
- u+x: Giving the owner executing permission.
- history_cs.sh: Name of the script.
./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
The Bash script displays the history using cat ~/.bash_history and then clears the history file using truncate -s 0 ~/.bash_history. -s 0 option of the truncate command makes reduce the length of the history file to zero hence clearing it.
Run the history_cs.sh script by the following command:
. ./history_ps.sh
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.
rm ~/.bash_history
- 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
The provided Bash script sets and displays the size of the history list in the current shell session. Initially, it prints the current value of the HISTSIZE variable, representing the maximum number of commands stored in the history list. Then, it updates HISTSIZE to 20, indicating that the history list will now store the last 20 commands. Finally, it prints the new value of HISTSIZE confirming the change.
Run the histsize.sh script by the following command:
./histsize.sh
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 |
---|---|
|
|
|
|
Conclusion
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
Related Articles
- 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]
<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners