Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 701
KarstenF Avatar asked Mar 12 '09 20:03

KarstenF


People also ask

How would you disable bash history using?

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.

How do I hide command 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.

Can you edit bash history?

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.


2 Answers

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)

like image 162
Node Avatar answered Sep 24 '22 05:09

Node


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

like image 30
T-Hawk Avatar answered Sep 22 '22 05:09

T-Hawk