14

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?

28

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
12

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*'
6

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

1

I usually use:

history -w; history -c; $EDITOR $HISTFILE; history -r

The history file is written to disk, cleared in memory, you edit it to do what you want, and then the history file you edited is read back in.

Note that if you editor saves backup files, the naughty word might still appear on disk.

Another option using -d is to use predictive history numbers:

startide sr> # good thing
startide sr> # bad thing
startide sr> history | tail -n 2
17239  # bad thing
17240  history | tail -n 2
startide sr> history -w; history -d 17239; history -d 17239; history -d 17239
startide sr> # other thing
startide sr> history | tail -n 3
17239  # good thing
17240  # other thing
17241  history | tail  -n 3

In this example, I delete the bad thing, I delete the command to get the history number, and then I delete the command that I used to delete the history. Everything is sanitized then.

0

If you want to run a command without saving it in history, prepend it with an extra space

prompt$ echo saved
prompt$  echo not saved \
> #     ^ extra space

For this to work you need either ignorespace or ignoreboth in HISTCONTROL. For example, run

HISTCONTROL=ignorespace To make this setting persistent, put it in your .bashrc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.