Each command executed in your Linux terminal is being recorded by your shell (referring to Bash) in a separate file (like “~/.bash_history
“) that you can view using the history command.
For most Linux distributions, the number of commands that should be recorded and the number of results that will be displayed on output are set using the “HISTFILESIZE
” and “HISTSIZE
” variables.
Execute the following command to check your history buffer:
$ echo "History Size:" $HISTSIZE, "History File Size:" $HISTFILESIZE
Output:
So, if you type “history” into your Linux terminal, you will see a list of the last “1000
” commands you have run.
$ history
Output:
In some cases, you may be running commands that require sensitive information, such as an email address, username, or password, and you do not want this information saved in the history.
This article will show you how to prevent a command from being saved in history and how to remove a command that has already been saved.
Tutorial Details
Description | Stop the History from Recording Commands |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | history |
Internet Required | No |
Deleting a Command from the History Soon After Execution
You can easily delete any command soon after its execution by appending the “history -d $(history 1)
” command to it.
Basically, here you are taking the latest entry from the history using “$(history 1)
“, where “1
” is the offset, and the “history -d
” command is used to delete it.
When you run a command, it is saved in the history, as shown.
$ echo "This command will be saved in the history"
$ history 5
Output:
However, when you append the “history -d $(history 1)
” command at the end of the existing command, it will immediately delete it soon after the command execution.
$ echo "This command will not be saved in the history";history -d $(history 1)
$ history 5
Output:
As you can see, this is the easiest and most efficient way to remove a command soon after its execution in the Linux system.
Deleting a Command with a Space at the Start
Another way to prevent the command from being saved in history is by using the “HISTCONTROL
” variable, whose value should be either “ignorespace
” or “ignoreboth
” for this method to work.
If the value for the variable is set to any of the mentioned values, then whenever the command is executed with a space in front of it, it fails to update in the history.
Execute any one of the following commands to check the value of the “HISTCONTROL
” variable.
$ echo $HISTCONTROL
#OR
$ grep $HISTCONTROL ~/.bashrc
Output:
The mentioned variable is set, so if you execute any command with a space at the start, it will not be saved in the history, as shown.
$ echo "This command will be saved in the history"
$ echo "This command will not be saved in the history"
Output:
This method is effective, but if you intend to execute multiple commands that should not be recorded in the history, then you might make a mistake while giving the space at the beginning of the command.
However, you can use the next method to disable commands from being recorded for a certain amount of time.
Preventing a Series of Commands from Being Saved in History
This is the best way to prevent the history from capturing the series of commands for a certain amount of time using the set command.
All you need to do is run the following command before the command sequence you don’t want to be recorded in the history:
$ set +o history
All the latest commands will not be saved in the history after executing the above command.
Once you are done with command execution that should not be in the history, execute the following command to enable the history recording for the future commands.
$ set -o history
From now on, all the latest commands will be saved in history.
This method is more effective than the previous two methods when you intend to stop the recording for multiple commands.
Ignoring Specific Commands from Being Saved in History
If you never want certain commands to be saved in the history, you can use the “HISTIGNORE
” variable to change your shell configuration file.
Simply modify the shell configuration file using your choice of text editor.
$ vim ~/.bashrc
And then add the following syntax at the end of the file to ignore the “passwd
“, “ftp
“, “cd
“, “ls
” and “ps
” commands separated with a “:
” colon from being saved in the history.
export HISTIGNORE="passwd*:ftp*:cd*:ls*:ps*"
Output:
Save and close the file, then reload the configuration changes using the source command.
$ source ~/.bashrc
Now, if you run one of the ignored commands, it won’t be recorded in your history.
This method is especially useful for not allowing specific commands to be saved in the history.
Removing Existing Commands from History
If you already executed the command that should not be present in the history list, then you can easily remove it by using its event number with the “history -d
” command.
For example, the following command will remove the command entry with the event number “5
” from the history record.
$ history -d 5
Output:
Instead of removing individual command entries, you can directly disable the history from recording any future commands by following the next method.
Disabling Commands from Being Recorded in History
There are two variables in the shell configuration file “~/.bashrc
” that determine the history buffer.
HISTSIZE
: It’s responsible for showing the defined number of results when the history command is executed.HISTFILESIZE
: It’s responsible for recording a certain number of user executed commands in the history file.
If you’ve decided not to save any future commands in the history, you can simply modify the shell configuration file using your preferred text editor, either Vim or Nano.
$ vim ~/.bashrc
Now, change the “HISTSIZE
” and “HISTFILESIZE
” values to zero, as shown.
Save and close the file, and reload the configuration changes using the following command.
$ source ~/.bashrc
Clear your existing history record using the following command.
$ history -c
All the commands you execute from now on will not be saved in history.
$ ls
$ pwd
$ history
Output:
In the future, if you want to enable history recording for executed commands, edit the shell configuration file and set “HISTSIZE=1000
” and “HISTFILESIZE=2000
“.
Final Tips!
All the methods mentioned in this article work effectively. Most of the time, I use the set command to prevent commands from being captured for a certain amount of time.
However, you can choose the one most relevant for you and also let me know your personal preferred command to hide execution from history in the comment section.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.