How to remove a single line from history?

  • I'm working in Mac OSX, so I guess I'm using bash...?

    Sometimes I enter something that I don't want to be remembered in the history. How do I remove it?

  • jw013

    jw013 Correct answer

    8 years ago

    Preventative measures

    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.

    Post-mortem clean-up

    If you've already run the command, and want to remove it from history, first use

    history
    

    to display the list of commands in your history. Find the number next to the one you want to delete (e.g. 1234) and run

    history -d 1234
    

    Additionally, if the line you want to delete has already been written to your $HISTFILE (which typically happens when you end a session by default), you will need to write back to $HISTFILE, or the line will reappear when you open a new session:

    history -w
    

    It worked. I think we need to source ~/.bashrc after modifying it...

    @BSeven Yes, all `bash` settings are stored in RAM while the shell is running. The `rc` files are only for storing persistent settings for the next time the shell starts.

    There are edge cases when this will not work, but under default configurations it should.

    @jordanm What edge cases are you talking about? Are they worth listing here?

    @jw013 I set PROMPT_COMMAND to `history -a`, in that case it is already written to the history file, rather than on exit under normal configuration. Specifically: http://mywiki.wooledge.org/BashFAQ/088

    but I don't want history -d command itself to be logged into history??

    What about clearing the current terminal window buffer? For example, if I run `history` to find the command in which I typed a secret command, I can use `history -d` to delete it but someone could simply scroll up my screen and see it.

    How to do the same in `zshrc` shell

    @KasunSiyambalapitiya See if this helps: https://unix.stackexchange.com/q/236094/9537

    @RohailAbbas - easy, you first enter `HISTCONTROL=ignorespace`, then invoke two `history -d` commands with a leading space - one for deleting your original command and one for deleting the ignorespace assignment.

    YMMV, but here's how I cleared all entries that match a grep: `while true; do str=\`history | grep -m 1 YOUR_GREP_HERE | awk '{print $1}'\`; if [[ ! $str ]]; then break; fi; history -d $str; done`

    `history -w ` does not exist oh RHEL 7 the reason why the commands still appear for zsh users is that you also have to delete the entry from .zsh_history AND very important to logout and login again.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM