How to Clear History in Bash [2 Practical Cases]

A Complete Guide for Beginners Enroll Course Now

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.

Steps to Follow >

❶ At first, launch an Ubuntu Terminal.

❷ Write the following command to open a file named in the build-in nano editor:

  • nano: Opens a file in the Nano text editor.
  • Name of the file.
Creating Bash script 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 ( >


# Display the history before clearing
echo "History before clearing:"

# 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:"

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
  • chmod: Changes permissions.
  • u+x: Giving the owner executing permission.
  • Name of the script.
Changing permission of Bash script file to clear history❺ Run the script by the following command:


Clearing history using Bash script 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.

Note: Clearing the history of the current shell doesn’t change the main history file.
Note: You can use the source command instead of the additional dot(.) to execute the script in the current shell. In other words source ./ executes the script in the same way as shown.

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.

You can follow the Steps of Case 1 to learn about creating and saving shell scripts.

Script ( >


# 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 script by the following command:

. ./

Clearing history from the history fileUpon 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.

You can follow the Steps of Case 1 to learn about creating and saving shell scripts.

Script ( >


# Current size of the history file
# Set new size
# New size of the history file

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 script by the following command:


Changing the size of the history fileThe 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
  • Clearing history refers to deleting the history of executed commands.
  • It clears the terminal screen and gives the appearance of a fresh and empty terminal window.
  • There is no shortcut of history command for clearing history.
  • CTRL+L shortcut serves the same purpose as 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 clear history in zsh?
Like Bash history is zsh is saved in ~/.zsh_history file. Delete or clear that file in order to clear history.

Can I delete history without any trace?
Yes, you can. Install wipe command and delete history using wipe. This will leave no trace in the system about deleting history.
How to clear last 10 lines from history?
You can clear last 10 lines of history file by iterating over the history file using a for loop.

Related Articles

<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial

Rate this post
Md Zahidul Islam Laku

Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio

Leave a Comment