Brand logo letters t and k
Post

How to Clear Your Bash History

Published
Updated

Introduction

Bash history is useful: it lets you search for and repeat previous commands, speeds up common workflows, and keeps a record of commands you've run on a server.

Over time, however, your history file can accumulate typos, failed attempts, and duplicate commands. You may eventually want to remove some or all of it.

The process is straightforward, but there's an important detail to handle first: Bash keeps history in memory during a session and may write it to the history file when the session ends.

This guide explains how to clear Bash history in an SSH session on Debian.

Clearing your Bash history

The following steps clear the history held by the current Bash session and empty the history file on disk.

Step 1: Clear the in-memory history for the current session:

history -c

Step 2: Empty the history file on disk:

truncate -s 0 ~/.bash_history

Step 3: Exit the session:

exit

history -c clears Bash's in-memory history, while truncate -s 0 empties the history file on disk. Running them in this order ensures that Bash has no old history to write back when you exit.

Deleting a single command

If you only need to remove one command, remove it from both the history file and Bash's in-memory history.

This method works best when only one SSH session is active and the command is a simple, single-line entry. Duplicate commands can make it difficult to identify the correct entry, while another active session could rewrite the history file as you edit it. If possible, close other SSH sessions first.

Step 1: Find the command in the history file:

grep -n "search_term" ~/.bash_history

The number printed before the command is its line number in the file.

Step 2: Delete that line from the file:

For example, the following command deletes line 42:

sed -i '42d' ~/.bash_history

Step 3: Find the command's history number in the current session:

history | grep "search_term"

Step 4: Delete the command from the in-memory history:

For example, the following command deletes history entry 142:

history -d 142

history -d takes the history number shown by history, not the line number from ~/.bash_history.

Step 5: Exit the session:

exit

How the commands work

truncate

truncate changes a file to the size you specify. The -s 0 option sets the file size to zero, emptying the file without deleting it.

Using truncate avoids relying on Bash's history-writing commands. For example, history -w writes the current in-memory history list to the history file. If you run it before clearing that list, it can write the history you want to remove back to the file.

sed

sed is a stream editor. The -i option edits a file in place. In this example, the expression '42d' selects line 42 (42) and deletes it (d).

sed is used here because it is installed by default on Debian and can delete a specific line without opening an interactive editor or manually reconstructing the file. Other tools, such as awk, perl, or an editor like vim, can do the same job, but sed expresses this particular operation concisely.

Editing the file alone is not enough because Bash keeps a separate in-memory history list. If the command remains in that list, Bash may write it back to the file when the session ends. That is why you must also remove it with history -d, used in step 4.

A note about cleared history

Clearing ~/.bash_history removes commands from that history file, but they may still exist in another shell's in-memory history, terminal scrollback, system or audit logs, backups, or centralized monitoring systems.

If a command contained a password, API key, or other secret, rotate or revoke it immediately. Clearing Bash history reduces local exposure, but it cannot guarantee that every copy has been erased.

Newsletter

Want more like this?

Drop your email below and we'll keep you posted.