Sometimes, when I run commands like rm -rf XYZ
, I don't want this to be recorded in Bash history, because I might accidentally run the same command again by reverse-i-search
. Is there a good way to prevent this from happening?
If you've set the HISTCONTROL
environment variable to ignoreboth
(which is usually set by default), commands with a leading space character will not be stored in the history (as well as duplicates).
For example:
$ HISTCONTROL=ignoreboth
$ echo test1
$ echo test2
$ history | tail -n2
1015 echo test1
1016 history | tail -n2
Here is what man bash
says:
HISTCONTROL
A colon-separated list of values controlling how commands are saved on the history list. If the list of values includes
ignorespace
, lines which begin with a space character are not saved in the history list. A value ofignoredups
causes lines matching the previous history entry to not be saved. A value ofignoreboth
is shorthand forignorespace
andignoredups
. A value oferasedups
causes all previous lines matching the current line to be removed from the history list before that line is saved. Any value not in the above list is ignored. IfHISTCONTROL
is unset, or does not include a valid value, all lines read by the shell parser are saved on the history list, subject to the value ofHISTIGNORE
. The second and subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value ofHISTCONTROL
.
See also:
In your .bashrc/.bash_profile/wherever you want, put export HISTIGNORE=' *'
. Then just begin any command you want to ignore with one space.
$ ls # goes in history
$ ls # does not
-
4This depends on the value of the
HISTCONTROL
environment variable, which does not necessarily haveignorespace
set. – sorpigal Jun 25 '11 at 18:53 -
3If
HISTCONTROL
containsignorespace
, it's actually only redundant to include ` *` inHISTIGNORE
. The two operate independently. – chepner Jul 19 '13 at 12:49
Even better use HISTIGNORE
. This allows you to specify a set of patterns to be ignored (such as rm
). It is better (I think) than just piping all history to /dev/null
.
kill -9 $$
I know that is not as best as the previous answers, but this will kill the current Bash shell without saving anything, useful when HISTCONTROL is not set by default, you forgot to set it, or pure and simple you forgot to put a leading space and you just typed in some passwords and don't want them to remain permanently in history.
This is the quick way, and something like erasing the history file is not as good because you need to do it outside a history saving shell (log in as different user and use su/sudo, creating a background job, etc.)
You can do one of two things:
export HISTFILE=/dev/null
Or, begin the command with a space.
-
5
-
8Some of us think auto-ignoring commands prefixed by a space is a really bad idea and therefore don't have
ignorespace
set by default. – sorpigal Jun 25 '11 at 18:57 -
3Setting
HISTFILE
this way prevents storing any history, and does not prevent the command from being added to the in-memory history. – chepner Jul 19 '13 at 12:50
Or
unset HISTFILE
(similar to the previous answer only shorter: export HISTFILE=/dev/null)
-
1
At shell startup, I explicitly cleanup the history from the entries that I don't want to be there. For example, I don't want any rm -rf
in the history (it's trauma after removing a directory full of results processed overnight, just with a single Arrow-Up
+ Enter
:)
I put the following snippet in my init file (works with .zshrc
, should also work with .bashrc
)
# ...
HISTFILE=~/.zshhistory
# ...
# remove dangerous entries from the shell history
temp_histfile="/tmp/$$.temp_histfile"
grep -v -P '^rm .*-rf' $HISTFILE > $temp_histfile
mv $temp_histfile $HISTFILE
bash
may be used for writing shell scripts (hence programming), this particular aspect of it has absolutely nothing to do with programming (history is an interactive-only thing). Voting to close. – paxdiablo Oct 27 '19 at 5:36