64

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?)

1
  • 1
    As per the deleted answer, it's worth using control-R for reverse history type-ahead search, instead of the up arrow. This helps reduce the chances of such mistakes, though it's still worth suppressing things from history. Dec 17, 2012 at 5:10

2 Answers 2

75

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)

1
  • 8
    If it doesn't work by default, add [ \t]* to HISTIGNORE.
    – Boden
    Mar 12, 2009 at 20:51
46

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

1
  • 20
    Multiple HISTCONTROL options can be set by separating them with colons, like: export HISTCONTROL=ignoredups:ignorespace
    – BlackShift
    Dec 10, 2012 at 8:38

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