Run a Linux Command Without Saving It in History

By default, every command that you execute on your terminal is put away by the shell (command interpreter) in a specific file called a set of history file or shell order history. In Bash (the most well known shell on Linux systems), by default, the number of commands endured in the set of history is 1000, and some Linux distributions have 500.

Execute the following command to check your history size in Bash:

$ echo $HISTSIZE

To see more older commands that you have run, you can use the set of history command to show the shell command history:

$ history

In some cases, you might need to disable the shell from logging commands to its command history. You can do it as follows.

Follow the below steps to Delete a Linux Command from History After Running:

You can erase an command promptly from the shell history in the wake of running it on the command line by appending the history - d $(history 1) command to it.

The $(history 1) sub-command recovers the most recent entry in the history in the ongoing terminal session, where 1 is the counterbalanced and the -d option helps to delete it.

Any command run regularly gets saved in the shell history.

$ echo "This command is saved in history"
$ history | tail

Be that as it may, when you append the history -d $(history 1) command to an command line, it straight away gets erased from the shell history.

$ echo "This command is not saved in history";history -d $(history 1)
$ history | tail

One more method for keeping the shell from saving an command in history is to prefix the command with a space. However, this relies completely upon the value of the $HISTCONTROL shell variable defined in the ~/.bashrc Bash startup file. Having one of these values: ignorespace or ignoreboth, for this method to work.

You can actually look at the value of the $HISTCONTROL variable as displayed.

$ echo $HISTCONTROL
OR
$ cat ~/.bashrc | grep $HISTCONTROL

In the event that the previously mentioned shell variable is set, any command prefixed with a space isn’t 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!"

Conclusion

I hope you have learned something from this article.

Now I’d like to hear your thoughts.

Was this guide useful to you?

Or maybe you have some queries.

Have I not included any command in this guide?

Leave a comment below.

Leave a Comment