Can you prevent a command from going into the bash shell command history?

LinuxBashUnixShell

Linux Problem Overview


Is there a way to prevent a command from being added to the bash shell's command history?

I would like to be able to prevent certain "dangerous" commands from being added to the history, such as "rm -rf ~/some/dir", so that it is not accessible to me by pressing the up-arrow to reach previous commands. In this way, it would not be possible to accidentally repeat one of these commands.

The reason I ask is that I use the up arrow a lot to access previous commands in the shell history, and have often caught myself about to hit enter on what I thought was the correct command, only to realise that I was about to do something stupid/annoying/dangerous. I don't like the idea of rms etc floating around in my shell history, waiting for me to step on them!

(Note: I am aware that one can set up patterns in HISTIGNORE, but what would be nice is something one can apply on a per-command basis, which would become good a habit. Unless there is a clever way to achieve this using HISTIGNORE that I have missed?)

Linux Solutions


Solution 1 - Linux

On newer Bash Versions you could simply add a space at the beginning of your command. :) If it doesn't work by default, add [ \t]* to HISTIGNORE. (As mentioned in the comments. thx)

Solution 2 - Linux

Add ignorespace to your HISTCONTROL environment variable. Then any command line that begins with a space won't be entered into your history.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKarstenFView Question on Stackoverflow
Solution 1 - LinuxNodeView Answer on Stackoverflow
Solution 2 - LinuxT-HawkView Answer on Stackoverflow