Home > Enterprise >  How to delete a single command from history in bash
How to delete a single command from history in bash

Time:09-22

After some searching online I found that the following command should remove a single entry from the bash history:

$ history -d <number>

So let's say I would like to remove this single line from my history because I misspelled git :

10003 gti add . && git commit -m

When I do the following command:

$ history -d 10003

It still shows up in my history, even when I restart the terminal

So any idea how I can fix this because I use autocomplete and sometimes especially when using git it can get a bit messy.

CodePudding user response:

Command history in Bash is stored both in-memory, typically for the current shell session, and on disk, by default in ~/.bash_history. A default history configuration might append the in-memory session to the file on disk when the session is terminated, but there are a lot of knobs to tweak.

In any case, to permanently delete a command from history, you might have to edit the ~/.bash_history file directly.

A few references to history settings:

  • HISTFILE – the file where history is persisted (defaults to ~/.bash_history)
  • HISTFILESIZE – the number of commands stored in the history file (defaults to HISTSIZE)
  • HISTSIZE – the number of commands stored in the (in-memory) history list (defaults to 500)
  • cmdhist – shell option to save multi-line commands in a single history entry
  • histappend – shell option to control if the history list is appended to the history file, or if the history file is overwritten
  • lithist – shell option to store multi-line commands using linebreaks instead of semicolons

CodePudding user response:

So turns out it's solved by deleting the command from ~/bash_history

  • Related