Clear Bash history in AlmaLinux/Ubuntu

Published: December 26, 2022

Last updated: December 26, 2022

Clear Bash history in AlmaLinux/Ubuntu

In this article, we cover how to clear Bash history in AlmaLinux and Ubuntu. All the commands, text we enter in the Bash terminal session are saved. So, next time we can use Up arrow and Down arrow keys to access the commands we have entered in the previous terminal session.

All the commands we enter are only saved to the Bash history file at the end of session. To know Bash history file, we can use the following command -

echo $HISTFILE

Unless we have made modifications to default configuration, the Bash history file for both the operating systems is - /home/$USER/.bash_history

Clear Bash history in AlmaLinux/Ubuntu

At any stage, during the terminal session if you don't want entered commands to end up in Bash history file or even be available in current session then issue the following in terminal -

history -c

What this does is, it would clear all commands we have entered from the start of terminal session. But, if we are in the middle of session then subsequent commands would show up in Bash history file. To remove those as well, again we have to issue history -c.

The commands from previous terminal sessions won't get cleared through this method.

We prefer to open the Bash history file i.e. /home/$USER/.bash_history in a text-editor like nano, vi, featherpad, gedit etc. and manually delete the command history from previous terminal sessions.

In conclusion, one should regularly clear Bash history if some critical information can be extracted through the commands entered in terminal sessions.

Additional Info -

If there are commands which we wish to save to bash history then, issue the following -

history -w

This would write all the commands we have entered from the start of terminal session till now to the Bash history file.

Apart from that, you would also notice that once we issue history -c then if we try to exit the terminal session through the command -

exit

exit command ends up in the Bash history file. Use key shortcut - Ctrl+D to exit if you don't want exit command to be stored there.