Open In App

Executing Linux Commands Without Storing Them in History

Last Updated : 24 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

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

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

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

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

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

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

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

Checking Existing History

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

set +o history
Disable history

Disable history

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

echo "Hello"
Run a command

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

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

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

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

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

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.

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 :

  1. Use “set +o ” to prevent saving commands indefinitely during a session
  2. Use space prefixing if you only want to run a few commands
  3. Use the “history -c” command if you only want to remove the entries of the current session
  4. Use other methods if you specifically require the features they provide


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads