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