Execute a Linux command without keeping it in history
For executing a command on our Linux operating system without keeping it in history, we can follow 4 possible methods that will help us to run commands without keeping them in history. Below are the methods which we will be discussing in this article:
- Method 1: By Prefixing with Space
- Method 2: By deleting the latest command
- Method 3: By setting flags to enable and disable history
- Method 4: By clearing the current session history
So now let’s move ahead and see each of the methods in a detailed and step-by-step manner :
Method 1: By Prefixing with Space
Whenever we prefix our commands with a space it triggers the HISTCONTROL variable. HISTCONTROL is a system-wide variable that dictates how commands are written into the bash_history file. On most Linux systems this value is set to ignoreboth or ignorespaces. In such a case HISTCONTROL prevents any command prefixed with a space from being written in the history in the first place. To use this method use the following steps :
Step 1: Using the keyboard shortcut “CRTL + ALT + T“, open the terminal and enter the below command to see the existing history of the terminal.
history
Step 2: Now run any command you wish to execute and make sure to prefix it with a space. An example would be as follows
echo "Hello"
Step 3: To recheck whether or not history the command is present in history, use the history command again
history
As we can see the command echo “World” which had been prefixed with a space does not get saved to our history. This is one of the easiest ways to execute Linux commands without keeping them in history.
Method 2: By deleting the latest command
Since commands are written to the history as soon as they are executed, we can delete the latest entry to prevent it from being visible later. For this, we use a combination of 2 commands “$(history -1)” which expands to become the last executed command and then we use “history -d ” which deletes this command.
Step 1: Using the keyboard shortcut “CRTL + ALT + T“, open the terminal and enter the below command to see the existing history of the terminal.
history
Step 2: Now run any command you wish to execute and make sure to add the following command “; history -d $(history 1)” at its end.
echo "Hello"; history -d $(history 1)
Step 3: To recheck whether or not history the command is present in history, use the history command again
history
As we can see here when echo “World” is used in combination with the command “history -d $(history -1)“, it no longer gets saved to the history. However, it should be noted that in this method the command does get saved once in the history but is removed quickly after.
Method 3: By setting flags to enable and disable history
Another way to prevent saving commands to history for a while is to use the “set +o history” command to disable the history from being saved and run “set -o history” to re-enable the saving of commands to history.
Step 1: Using the keyboard shortcut “CRTL + ALT + T“, open the terminal and enter the below command to see the existing history of the terminal.
history
Step 2: Run the set +o history command to disable the history
set +o history
Step 3: Now run any command you wish to execute. For Example
echo "Hello"
Step 4: To recheck whether or not history the command is present in history, use the history command again
history
Step 5: To re-enable history run the set -o history command
set -o history
As we can see from the example above, the command executed after “set +o history” does not get saved in the memory, but when we re-enable the saving of history using “set -o history” and run the echo “Hello” command, it does get saved to the memory.
Method 4: By clearing the current session history
We can also use the “history -c” command which allows us to clear the history of our current session using a command, to prevent the commands of the current session from being written to the memory. It should be noted that while this command clears the history of the current session, it does not clear the commands that will be written after it, nor will it prevent the saving of history in any new session that we might create. This command must be run at the end of any terminal session to prevent saving it to memory.
Step 1: Using the keyboard shortcut “CRTL + ALT + T“, open the terminal and enter the below command to see the existing history of the terminal.
history
Step 2: Now run any command you wish to execute and make sure to add the following command “; history -c” at its end. Example
echo "Hello"; history -c
Step 3: To recheck whether or not history the command is present in history, use the history command again
history
As we can see, no command is saved in the history that was executed before the “history -c” command. But the commands we execute afterward in this terminal session or other terminal sessions will get saved.
Executing Linux Commands Without Storing Them in History
In Linux, a lot of work is done via the command line or terminal. However, sometimes we might run a command that might contain some sensitive information or we just do not want to clutter our history file with unnecessary commands. In such cases, we want to execute Linux commands without keeping them in memory. There are multiple ways to achieve this behavior, and we will be discussing them in the following article