- How do I close a terminal without saving the history?
- 6 Answers 6
- Option 0
- Option 1
- Option 2
- Option 3
- Option 4
- How to Run a Linux Command Without Saving It in History
- Delete a Linux Command from History After Running
- How to Run a Linux Command Without Keeping It in History
- Tutorial Details
- Deleting a Command from the History Soon After Execution
- Deleting a Command with a Space at the Start
- Preventing a Series of Commands from Being Saved in History
- Ignoring Specific Commands from Being Saved in History
- Removing Existing Commands from History
- Disabling Commands from Being Recorded in History
- Final Tips!
How do I close a terminal without saving the history?
More than once I’ve accidentally run a number of commands and polluted my bash history. How do I close my terminal without saving my bash history? I’m using Fedora.
6 Answers 6
There’s more than a few ways to accomplish this.
Note: These options are not mutually exclusive; they can be used in any combination, or all at once.
Option 0
Delete the HISTFILE shell variable.
According to the man page this will remove the shell’s ability to automatically write any history to a file, regardless of if you set HISTFILE to something after unsetting it.
Option 1
If you’re a perfectionist when it comes to cluttering up your history file, then what you can do is modify the HISTIGNORE variable to include globs of commands you don’t want recorded. For instance, if you add HISTIGNORE=’ls*:cd*’ to your ~/.bashrc then any instance of ls and cd aren’t inserted into your history file.
Note: If you put shopt -s extglob in your .bashrc , you can use extended globs in this variable, e.g:
This will ignore cd .. , cd ../ , cd ../.. , etc but leave cd myfolder
Option 2
If you want to control on a command-by-command basis what commands get left out of your history, you can set HISTCONTROL=’ignorespace’ which will omit any command lines starting with a space. Using ignoreboth will also omit repeated lines. Then, hitting the space bar before you enter a command will cause it to not show up in your history file.
# With HISTCONTROL=ignorespace (or =ignoreboth) this line gets written to history $ mycommand # But this line does not $ mycommand
Option 3
If you just want to make it so when you close the terminal the shell exits immediately, you can trap the signal the terminal program sends the shell ( xterm , for instance sends SIGHUP then waits for the shell to exit) and make exit without saving the history when it receives this signal. Add this to your ~/.bashrc :
# don't record history when the window is closed trap 'unset HISTFILE; exit' SIGHUP
Option 4
Sending SIGKILL to your shell’s PID will kill your shell right away without the shell being able to do anything such as trap the signal, save history, execute ~/.bash_logout , warn about stopped jobs, or any of that good stuff.
( $$ expands to the PID of the shell process executing the command)
How to Run a Linux Command Without Saving It in History
By default, every command that you execute on your terminal is stored by the shell (command interpreter) in a certain file called a history file or shell command history. In Bash (the most popular shell on Linux systems), by default, the number of commands persisted in the history is 1000, and some Linux distributions have 500.
To check your history size in Bash, run this command:
To see older commands that you have run, you can use the history command to display the shell command history:
Sometimes, you may want to disable the shell from logging commands to its command history. You can do it as follows.
Delete a Linux Command from History After Running
You can delete a command immediately from the shell history after running it on the command line by appending the history -d $(history 1) command to it.
The $(history 1) sub-command retrieves the latest entry in the history in the current terminal session, where 1 is the offset and the -d option helps to delete it.
Any command run normally gets saved in the shell history.
$ echo "This command is saved in history" $ history | tail
However, when you append the history -d $(history 1) command to a command line, it straight away gets deleted from the shell history as shown in the following screenshot:
$ echo "This command is not saved in history";history -d $(history 1) $ history | tail
Another way to prevent the shell from saving a command in history is to prefix the command with a space. But this depends fully on the value of the $HISTCONTROL shell variable defined in the ~/.bashrc Bash startup file. It should be set to have one of these values: ignorespace or ignoreboth, for this method to work.
You can check the value of the $HISTCONTROL variable as shown.
$ echo $HISTCONTROL OR $ cat ~/.bashrc | grep $HISTCONTROL
If the aforementioned shell variable is set, then any command prefixed with a space is not saved in the history:
$ echo "This command is not prefixed with space, it will be saved in history!" $ echo "This command is prefixed with space, it will not be saved in history!"
Here are some other interesting articles about the Bash history and history commands:
That’s it for now! Use the comment form below to share your thoughts with us concerning this topic. Until next time, stay with us.
How to Run a Linux Command Without Keeping It in History
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
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.
Tutorial Details
Description | Stop the History from Recording Commands |
Difficulty Level | Low |
Root or Sudo Privileges | No |
Host System and Architecture | Ubuntu 22.10 (x64) |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | history |
Internet Required | No |
Discussed Tools in this Article |
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 “ HISTCONTROL ” variable.
$ 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
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*"
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.
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.
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 “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.