How to clear shell history?

QuestionsQuestions8 SkillsProDec, 11 2025
0137

That's another excellent question! Sometimes you might want to clear your shell history for privacy, security, or just to tidy things up.

There are a few ways to clear your shell history:

  1. Clear history for the current session and remove from history file (most common and thorough):

    history -c
    history -w
    • history -c: This command clears the command history from the current session's memory. This means if you type history right after this, it will show an empty list (or start from 1 again).
    • history -w: This command writes the current in-memory history (which is now empty) to the history file (~/.bash_history). This effectively overwrites the history file with an empty set of commands.
  2. Delete the history file directly (less graceful but effective):

    rm ~/.bash_history

    This command directly deletes the history file. However, your current session's history in memory will still exist until you exit the terminal or use history -c. If you delete the file and then exit, your current session's history will be written to a new .bash_history file. So, often history -c followed by rm ~/.bash_history (or history -w to clear the file) is more complete if you want no trace.

  3. Prevent commands from being saved in history temporarily:
    If you prepend a command with a space, it typically won't be saved in your history (this depends on the HISTCONTROL shell variable, which is usually set to ignore space-prefixed commands by default).
    Example:

     mysecretcommand --with-sensitive-data

    (Notice the space before mysecretcommand)

Choose the method that best suits your needs. Using history -c followed by history -w is generally the safest and most complete way to clear the history both from memory and from the file for the current user.

0 Comments

no data
Be the first to share your comment!