Clear all bash history by using history command:
Run the following command to create some bash history. ‘date’ command will display the current date and time. ‘ls’ command will display the list of the files and folders of the current location. ‘clear‘ command will clear the terminal screen.
$ ls
$ clear
Run the history command to display the current bash history.
Run the following command to clear the terminal history and exit from the terminal.
Clear specific bash history entry by using history command:
Run the following command to create some bash history. The first command will print ‘Hello’ message. The second command will print the current logged in user’s name. The third command will take input from the user and store in the variable $a. The fourth command will print the value of $a.
$ who
$ read a
$ echo $a
Run the ‘history’ command to display the current history.
Run the following commands to delete the 4th entry of the history and print the history after delete.
$ history
Here, the entry of ‘echo $a‘ is removed from the history entry.
Clear all history by removing .bash_history:
If the ~/.bash_history file exists and stores the history information in that file, then you can run the following command to remove the file.
Prevent storing history information permanently:
Run the following unset command to prevent creating a history file and exit from the terminal. If you open a new terminal after running the following command, then no previous history information will display.
When the value of HISTSIZE is set 0, then no history entry will be stored permanently. The following command will stop storing history information and terminate the terminal. When a new terminal is opened after running this command, then no previous history information will display.
If you want to remove the history file forcefully, prevent creating a history file, and terminate from the terminal, then run the following command. After that, if a new terminal is opened, then it will work from the blank history.
The following command can also be used for deleting the current history information permanently and terminate from the terminal. When a new terminal is opened after running this command, then no previous history information will display.
Conclusion:
This article shows how bash history can be cleared and prevent storing history information permanently by using various bash commands. If the bash users work with normal bash commands, then he/she can use the history commands mentioned above to remove particular or all history information when requires. But if the users work with sensitive data, then it is better to select those commands shown in this article to prevent storing history information permanently.