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

Checking Existing 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"

Prefix commands with space

Step 3: To recheck whether or not history the command is present in history, use the history command again

history

Verify if the command is present in the 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

Checking Existing 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)

Running a command, and deleting the latest entry in history

Step 3: To recheck whether or not history the command is present in history, use the history command again

history

Verify if the command is present in the 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

Checking Existing History

Step 2: Run the set +o history command to disable the history

set +o history

Disable history

Step 3: Now run any command you wish to execute. For Example

echo "Hello"

Run a command

Step 4: To recheck whether or not history the command is present in history, use the history command again

history

Verify if the command is present in the history

Step 5: To re-enable history run the set -o history command

set -o history

Re-enable 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

Checking Existing 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

Clearing session history after running command

Step 3: To recheck whether or not history the command is present in history, use the history command again

history

Verify if the command is present in the 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

Similar Reads

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:...

Conclusion

Overall there are multiple ways to prevent history from being saved while running a command, or deleting the history after it has been recorded. Based on your needs one or more options might be feasible, so choose the one that fits your needs best. But as a general rule of thumb :...

Contact Us