138

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jeeyoung Kim
  • 5,827
  • 8
  • 44
  • 54

8 Answers8

181

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 of ignoredups causes lines matching the previous history entry to not be saved. A value of ignoreboth is shorthand for ignorespace and ignoredups. A value of erasedups 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. If HISTCONTROL 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 of HISTIGNORE. The second and subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value of HISTCONTROL.

See also:

Community
  • 1
  • 1
kenorb
  • 155,785
  • 88
  • 678
  • 743
65

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
rhdoenges
  • 709
  • 1
  • 4
  • 4
  • 4
    This depends on the value of the `HISTCONTROL` environment variable, which does not necessarily have `ignorespace` set. – sorpigal Jun 25 '11 at 18:53
  • 4
    If `HISTCONTROL` contains `ignorespace`, it's actually only redundant to include ` *` in `HISTIGNORE`. The two operate independently. – chepner Jul 19 '13 at 12:49
25

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Blake Mathman
  • 2,699
  • 1
  • 23
  • 20
23
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.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
THESorcerer
  • 989
  • 9
  • 19
12

You can do one of two things:

export HISTFILE=/dev/null

Or, begin the command with a space.

Lesmana
  • 25,663
  • 9
  • 82
  • 87
Corey Henderson
  • 7,239
  • 1
  • 39
  • 43
4

Or

unset HISTFILE

(similar to the previous answer only shorter: export HISTFILE=/dev/null)

aledra
  • 89
  • 5
3

I added an "Incognito" functionality to my .bashrc for when I want to run some commands without being saved without having to add spaces before each one.

Do note though that the in-memory history of the current terminal session will still be saved, but when I open a new terminal the commands issued in a past terminal's incognito session will never be seen because they were never written to the HISTFILE.

To your .bashrc:

ignoreHistory="false"
DEFAULT_HISTFILE=~/.bash_history
HISTFILE="$DEFAULT_HISTFILE"

# Toggle incognito mode
incognito() {
    if [[ "$ignoreHistory" == "true" ]]; then
        echo -e "\e[33mExited incognito mode\e[39m"
        ignoreHistory="false"
        HISTFILE="$DEFAULT_HISTFILE"
    else
        echo -e "\e[33mEntered incognito mode\e[39m"
        ignoreHistory="true"
        HISTFILE=/dev/null
    fi
}

Nice little utility I think some people may find use in, you can even change the prompt to reflect whether you're in incognito mode or not.

Bassam Helal
  • 380
  • 3
  • 12
  • 2
    When toggling back, it seems that the whole history still gets written... even the commands you wrote just after entering incognito mode. At least that is what's happening when using zsh (I had to replace .bash_history with .zsh_history in your code). So I would avoid witching back to non incognito until closing the window or simply delete the else branch. – dgmz Jan 15 '22 at 14:22
  • to make the toggle back work, probably want to track the number of commands entered (using $HISTCMD or $LINENO, depending on the settings) and then use `history -d ` to delete the lines. Or just use `history -c` and wipe the whole list from memory. – SpinUp __ A Davis Jul 22 '22 at 18:14
  • would `exec $SHELL` be a good 'exit incoginto' method here? – Lockszmith Jun 07 '23 at 12:25
2

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
Jakub M.
  • 32,471
  • 48
  • 110
  • 179