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 rm
s 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?)
How to permanently disable bash history using set command. Again add set +o history to the end of to a new /etc/profile. d/disable. history.
Insert a space in the beginning of a command, and it will be ignored by the shell, meaning the command won't be recorded in history. However, there's a dependency – the said solution will only work if the HISTCONTROL environment variable is set to “ignorespace” or “ignoreboth,” which is by default in most cases.
Bash history is usually stored in the file ~/. bash_history. It enables you to recall and reuse the stored record in an efficient way to get the best out of bash history saving feature. Not only this, but you can also customize and control the bash command output in the way you want.
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)
Add ignorespace to your HISTCONTROL environment variable. Then any command line that begins with a space won't be entered into your history.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With