This question already has an answer here:

What's the easiest way to prevent dangerous commands matching a pattern (like rm) from going into the Bash command history? I'd like to keep them away from the history of the current session, not just from the .bash_history file.

marked as duplicate by agc, Jeff Schaller bash Apr 26 at 16:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


OK, seems there have been a few somewhat similar questions. The answer is to use:

export HISTIGNORE="rm*"

(It seems to be using glob syntax, and multiple patterns can be given by separating with colons :)

Another way is to set:

export HISTCONTROL=ignorespace

which will cause Bash to keep all commands starting with a space out of the history. So if you can get into the habit of typing space-rm-whatever this will work fine.

To keep both space-commands and duplicate commands out of the history, you can do:

export HISTCONTROL=ignoreboth

It's also possible to use both HISTIGNORE and HISTCONTROL.

Not the answer you're looking for? Browse other questions tagged or ask your own question.