Remove a certain line from Bash history file

You can achieve removal from the history file using the commandline in two steps:

  1. Typing history -d <line_number> deletes a specified line from the history in memory.
  2. Typing history -w writes the current in-memory history to the ~/.bash_history file.

The two steps together remove the line permanently from the in-memory history and from the .bash_history file as well.


You need to log out and back in or run history -a so the current history is committed to disk.

Then just edit the file ~/.bash_history.


To prevent a command from being added to the history in the first place, make sure that the environment variable HISTCONTROL contains among its colon-separated values the value ignorespace, for example (add e.g. to .bashrc):

$ export HISTCONTROL=ignorespace

This will prevent any command with a leading space from being added to the history. You can then clear the history completely by running

$  history -c -w
  ^-- additional space character