Have you ever run a command in the terminal and then wished it had not been saved in your history? As a Linux system administrator, keeping certain commands out of the history can be important for security or privacy. Below, we will show you several effective techniques for doing this, ensuring that only you know which commands you have run.
Why Would You Not Want a Command Saved in History?
The terminal is a powerful tool for any sysadmin. However, sometimes we run commands that we do not want recorded in the history, such as:
-
Commands containing passwords.
-
Commands that modify sensitive settings.
-
Commands you ran by mistake and would rather not keep.
Starting with a Blank Space
A simple and quick way to prevent a command from being saved in Bash history is to start it with a blank space. For this to work, you need to configure Bash appropriately. Make sure you have this line in your .bashrc or .bash_profile file:
export HISTCONTROL=ignorespace
Then, whenever you want to run a command without saving it in history, simply start it with a space:
comando_secreto
Temporarily Disabling History
Another option is to temporarily disable history before running the command. This is useful when you need to run several commands in a row without recording them.
set +o history
comando_secreto
set -o history
Using HISTIGNORE
If there are specific commands that you always want to exclude from history, you can use the HISTIGNORE variable. Add the commands you want to ignore to this variable:
export HISTIGNORE='comando_secreto:otro_comando'
This way, whenever you run comando_secreto or otro_comando, they will not be saved in history.
Redirecting History to /dev/null
If you want to ensure that a command is not recorded during a specific session, you can temporarily redirect the history file to /dev/null.
HISTFILE=/dev/null comando_secreto
Deleting the Last Command from History
If you have already run a command and realize that it should not have been saved, you can delete the last command from history using history -d:
comando_secreto
history -d $(history 1)
Using builtin command
To prevent a command from being saved in history, you can also use the builtin command:
builtin command comando_secreto
Summary
Each technique has its own uses and advantages. Here is a quick summary:
-
Blank Space: Quick and easy; just make sure
HISTCONTROLis configured correctly. -
Temporarily Disable History: Ideal for running multiple commands.
-
HISTIGNORE: Perfect for repetitive commands that you always want to exclude.
-
Redirect to /dev/null: Useful for specific sessions where you do not want to save any commands.
-
Delete the Last Command: A quick solution when you notice a mistake after running the command.
-
builtin command: An alternative way to prevent history logging.
With these techniques, you can ensure that sensitive commands are not saved in Bash history, keeping your terminal work more secure and private. Try these options and find the one that best fits your needs.
Do you use any other techniques to manage your Linux terminal history? Leave a comment and share your experience.

