Hide "history -d" in bash history

If I accidentally type my password or anything else sensitive in bash I can easily remove that line with history -d ROW#, but I'm always left with the history -d ROW# command in history showing everyone that someone corrected a mistake.

Can I append something to a command to prevent it from appearing in bash history?


To not save a single command in your history just precede it with a space (marked with here):

$ echo test
test
$ history | tail -n2
 3431  echo test
 3432  history | tail -n2
$ echo test2
test2
$ history | tail -n2
 3431  echo test
 3432  history | tail -n2

This behaviour is set in your ~/.bashrc file, namely in this line:

HISTCONTROL=ignoreboth

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.

ignoredups by the way is the reason why history | tail -n2 appears only once in the history in the above test.


A terminal's history is saved in the RAM and flushed to your ~/.bash_history as soon as you close the terminal. If you want to delete a specific entry from your ~/.bash_history you can do so with sed:

                                   # print every line…
sed '/^exit$/!d' .bash_history     # … which is just “exit”
sed '/^history/!d' .bash_history   # … beginning with “history”
sed '/>>log$/!d' .bash_history     # … ending with “>>log”
sed '\_/path/_!d' .bash_history    # … containing “/path/” anywhere

In the last one I changed the default delimiter / to _ as it's used inside the search term, in fact this is equal to sed -i '/\/path\//d' .bash_history. If the command outputs only the lines you want to delete add the -i option and change !d to d to perform the deletion:

                                   # delete every line…
sed -i '/^exit$/d' .bash_history   # … which is just “exit”
sed -i '/^history/d' .bash_history # … beginning with “history”
sed -i '/>>log$/d' .bash_history   # … ending with “>>log”
sed -i '\_/path/_d' .bash_history  # … containing “/path/” anywhere

One way would be to set the HISTIGNORE environment variable. From man bash

HISTIGNORE
      A colon-separated list of patterns used to decide which  command
      lines  should  be  saved  on  the history list.  Each pattern is
      anchored at the beginning of the line and must  match  the  com‐
      plete  line  (no  implicit  `*'  is  appended).  Each pattern is
      tested against the line after the checks specified  by  HISTCON‐
      TROL  are  applied. 

[FWIW, it is the default HISTCONTROL that provides the type-a-space-in-front workaround].

So, for example

HISTIGNORE='history -d*'

If you want it to be persistent, export it from your ~/.bashrc

export HISTIGNORE='history -d*'

You can

  • Close the terminal window(s). This will flush the most current history in the terminal window that you close latest (after the other windows are closed) into the file /home/$USER/.bash_history

  • Edit the file /home/$USER/.bash_history without involving bash, for example by starting your favourite editor via Alt+F2

    This way you can inspect the whole history and remove whatever commands, that you no longer want to be there.

    enter image description here

    enter image description here