run linux command without history

How to Run Linux Commands Without Logging in History

By default, every Linux system stores the commands that you execute in your terminal shell in a history file. It is useful to find and re-run past commands that might be really long. It is also useful for system administrators to keep track of the various commands that are being run on their system. But sometimes you may want to run Linux commands without logging in history. In this article, we will learn how to execute Linux commands without saving them to shell history.


How to Run Linux Commands Without Logging in History

To view your shell history, run the following command.

$ history

Please note, the above command will only show the shell commands that the presently logged user has run. It won’t show the past commands of other users, unless you switch to that user, as a root or sudo user.

Delete Linux Command from History After Running

The logging of commands into shell history is an automated process and cannot be prevented in most cases. If you run a command, it will be saved in history file. What you can do is to delete it immediately after you run it, so that it is erased from history. Here is a command that does so.

$ history -d $(history 1)

In the above command, we use -d option with history command to delete an entry from history file. $(history 1) gets you the latest entry in history file, which is deleted using -d option.

You can also append the following string to your shell command, so that you don’t have to run it separately.

;history -d $(history 1)

For example the following commands will echo a string, delete its entry from history and then display the latest history for you to verify the deletion.

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

Alternatively, you can also set the value of system variable $HISTCONTROL in ~/.bashrc file to ignorespace or ignoreboth. This will force the shell to ignore commands starting with space from being recorded in history file.

$ vi ~/.bashrc

Check the value of $HISTCONTROL. If it is not present, set it by adding the following line.

HISTCONTROL=ignorespace

If you set it to ignoreboth, then it will ignore commands prefixed with space as well as duplicate entries from being stored in history.

Save and close the file. Reload bash profile to apply changes.

$ source ~/.bashrc

Now if you prefix a command with space, as shown below, it will not be stored in history.

$ echo "hello world"
$ history | tail

In this article, we have learnt how to run Linux commands without logging into history.

Also read:

How to Get Column Names in Pandas DataFrame
How to Change NGINX AutoIndex
How to Manage User Password Expiry & Aging in Linux
How to Remove Yum Repositories
How to Undo or Redo Yum Install in RHEL/CentOS/Fedora

Leave a Reply

Your email address will not be published.