10
Ways to Clear Bash Command Line History in Linux
Bash has a record of all the commands typed in the ~/.bash_history file.
This is helpful however, it keeps sensitive commands such as passwords, server
addresses, tokens, and scripts. To prevent security breaches and other privacy
concerns, you may want to clear, edit or disable your bash history.
This tutorial explains all safe and recommended steps to delete bash
history in a clear and straightforward manner using commands in Linux.
1.
Clear Current Session Bash History
This command clears the in-memory history of the current terminal
session.
#history -c
History -c removes all commands stored in memory but does not remove
.bash_history.
2. Clear
History and Write Changes to .bash_history
This command clears both session memory and the history file.
#history -c && history -w
· History
-c clears current sessions
·
History
-w writes the empty history to disk
This completely wipes your bash history.
3.
Manually delete the .bash_history File
Remove the history file from your home directory. On the next login,
bash will create a fresh file.
#rm ~/.bash_history
4. Truncate
the .bash_history File
Instead of destroying the history file set the file size to zero. It is
safer than deleting the history file. This clears the file contents without
deleting the file itself.
#truncate -s 0 ~/.bash_history
5.
Clear History before Logout
For better privacy, the best practice is to clear the history right
before closing the terminal. It prevents other users from reading your history
later. #history -c && history -w
Or
#history -w

6.
Disable Bash History Completely
It prevents bash from storing anything. Edit the ~/.bashrc file, add
unset HISTFILE, and save the ~/.bashrc file. Bash won’t write a history file
anymore.
#sudo nano ~/.bashrc
#unset HISTFILE
#source ~/.bashrc
7.
Disable History Temporarily for a Session
It disables history recording in the current session and is useful for
short, sensitive tasks like passwords or API keys.
#set +o history
Turn it back on:
#set -o history
8. Clear
Specific Command from History
It deletes a single command by its ID and permits some sort of cleanup
without wiping everything.
#history -d <line_number>
#history -d 2
9. Prevents
Sensitive Commands from Being Saved
Using the default settings of HISTCONTROL=ignorespace is enabled, bash
wouldn’t save commands starting with a space. Ensure this in ~/.bashrc file.
#export HISTCONTROL=ignoredups: ignorespace
10.
Use Secure Erase for Maximum Privacy
Overwrite the history file to prevent recovery, which is useful for
servers or security-sensitive environments.
#shred -u ~/.bash_history
Verify
Bash History
To verify that bash history is clear, use the 'history' command. If
cleared, it shows nothing or only the history command itself.
#history
Summary
Table
|
Method
|
Description
|
Safe
|
|
History -c
|
Clear session history
|
Yes
|
|
History -c &&
history -w
|
Fully erase all history
|
Yes
|
|
rm ~/.bash_history
|
Delete file
|
Safe
|
|
Truncate file
|
Empty file without
deleting
|
Yes
|
|
Disable history
|
Permanent no-logging
|
Very secure
|
|
History -d
|
Delete specific command
|
Yes
|
|
Shred -u ~/.bash_history
|
Secure delete
|
Strong security
|
That’s all for now. Clearing your bash command line history is essential
for protecting your privacy, maintaining security and preventing sensitive data
leaks. The methods above give you full control to manage bash command line
history efficiently. Keep visiting seeklinux for more updates and tips, give your comments below, and feel free to contact us.
Post a Comment