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 “
Execute the following command to check your history buffer:
$ echo "History Size:" $HISTSIZE, "History File Size:" $HISTFILESIZE
So, if you type “history” into your Linux terminal, you will see a list of the last “
1000” commands you have run.
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.
|Description||Stop the History from Recording Commands|
|Root or Sudo Privileges||No|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
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
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
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 “
$ echo $HISTCONTROL #OR $ grep $HISTCONTROL ~/.bashrc
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"
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
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
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.
$ 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
In the future, if you want to enable history recording for executed commands, edit the shell configuration file and set “
HISTSIZE=1000” and “
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.