How to prevent a command from getting stored in history in Linux

Q: Is it possible to avoid commands getting stored in history in Linux?

A: Yes, you can do so in bash shell using the below method

In bash there is a variable HISTCONTROL which can be used to achieve our task. By default it contains a value of "ignoredups" as per which history command avoids keeping duplicate command records.

Let us change it to something else
# export HISTCONTROL=ignorespace

Let us verify if the value is changed
# echo $HISTCONTROL
ignorespace

Now as per this any command you run starting with a space will not be monitored or stored in history or and file defined under HISTFILE which by default is ~/.bash_history.

Let us try to run some commands with a space

Output of history before running command
# history | tail -5
412 echo $HISTCONTROL
413 cd /etc/httpd/conf
414 vi httpd.conf
415 /etc/init.d/httpd status
416 history | tail -5

Run any command starting with an extra space

#   date
Verify your results
# history | tail -5
413 cd /etc/httpd/conf
414 vi httpd.conf
415 /etc/init.d/httpd status
416 history | tail -5
417 history | tail -5

So as you see the date command executed is not stored.

Making the changes permanent
Now the variable change made is temporary and terminal based which means next time you login the HISTCONTROL variable will return to its default value.

To make it permanent follow the below link
How to set environment (PATH) variable permanently in Linux

Related Articles
View the history of commands along with time
Long list the files on the basis of time