Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minuteBash keeps a record of commands you run so you can recall them quickly, but that convenience can become a problem if a command contains a password, token, private path, or simply something you do not want saved. Removing one command is usually straightforward, as long as you delete the correct history entry and update the saved history file afterward.
The main steps are to locate the command’s history number, remove that entry with Bash’s built-in history -d command, and then write the cleaned history back to disk with history -w. This matters because Bash history exists both in memory for the current shell and, usually, in a file such as ~/.bash_history.
As an Amazon Associate I earn from qualifying purchases.
There are a few caveats: history numbers can shift after deletion, other open terminal sessions may still have their own copies of the command, and history cleanup is not a complete privacy or security solution. If a secret was exposed, deleting the command from history is only one cleanup step; the secret should also be rotated or revoked.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchHow Bash history stores commands
Bash keeps command history in two places: an in-memory list for the current shell session and a history file on disk, usually ~/.bash_history. When you run commands in an interactive Bash shell, they are first added to the session’s internal history list. That list is what you see when you run history, press the Up arrow, or search with Ctrl + r.
#1 Best Overall
The saved history file is separate. By default, Bash reads ~/.bash_history when an interactive shell starts, loading previous commands into memory. When the shell exits normally, it writes the current session’s history back to that file. This means deleting a command from the visible history list does not automatically guarantee it has been removed from disk unless you also write the updated list back to the history file.
Several shell variables control this behavior. HISTFILE points to the file Bash uses for saved history. HISTSIZE controls how many commands Bash keeps in memory, while HISTFILESIZE controls how many lines are kept in the saved history file. You can inspect these settings with:
echo "$HISTFILE"to see the active history file pathecho "$HISTSIZE"to see the in-memory history limitecho "$HISTFILESIZE"to see the saved history file limit
History numbering is also session-specific. The numbers shown by history are indexes in the current shell’s in-memory list, not permanent line numbers in ~/.bash_history. If you open another terminal, it may show different numbers, especially if commands have been added, deleted, or loaded in a different order. For that reason, when deleting a single command, you should identify the entry in the same shell where you intend to run the deletion command.
Some systems are configured to append history after each command or to merge history across sessions using settings such as PROMPT_COMMAND='history -a; history -n', histappend, or custom shell startup files. In those setups, a command may reach ~/.bash_history before the terminal closes, and other open shells may later reload or rewrite it. This is one reason a complete cleanup often requires both deleting the in-memory entry and updating the saved history file.
Bash history is a convenience feature, not a secure audit log or a reliable privacy boundary. Commands may also appear in terminal scrollback, process accounting, shell session recorders, log files, editor swap files, clipboard managers, or monitoring tools. Removing one command from Bash history only addresses Bash’s own command list and history file; it does not erase every possible trace from the system.
Find the history entry you want to delete
Before you can remove a single Bash history item, you need its current history number. Bash assigns a number to each entry in the in-memory history list for the current shell session. Display the list with the built-in history command:
history
The output shows numbered commands, usually in ascending order. A typical entry may look like this:
Recommended Free Tools
1042 curl -H "Authorization: Bearer abc123" https://api.example.com/users
In this example, 1042 is the history number you would use with history -d. The number is not part of the command itself; it is Bash’s current index for that command in this shell. Be careful not to copy the command and run it again while inspecting it, especially if it contains credentials, destructive flags, or production hostnames.
Search history for a specific command
If your history is long, filter it with grep. For example, to find commands containing password:
history | grep password
For a case-insensitive search, use:
history | grep -i password
You can also search for part of a host name, token prefix, file path, or program name:
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Rank #2
- Used Book in Good Condition
history | grep sshhistory | grep example.comhistory | grep Authorizationhistory | grep rm
If the command contains shell metacharacters, quotes, brackets, or other special characters, search for a small plain-text fragment rather than the full command. For instance, instead of pasting an entire command containing a secret token, search for the command name or destination host.
Show only recent history
When the command was run recently, limit the output to the last set of entries:
history 20
This prints the most recent 20 commands with their history numbers. Increase the number if needed, such as history 100 or history 200. This is often safer and easier than scanning thousands of entries.
Use reverse search carefully
Bash also supports interactive reverse search with Ctrl-r. Press Ctrl-r, type part of the command, and Bash will show a matching history entry. This is useful for identifying the exact command text, but it does not show the history number directly. Once you find the command text, run history | grep 'search-text' or history 100 to locate the numbered entry.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Do not press Enter during reverse search unless you intend to run the matched command again. Press Ctrl-g or Esc to cancel the search and return to the prompt without executing anything.
Confirm the exact entry number
History numbers can change after deletion, after reading or writing history, or between separate terminal windows. Always use the number shown in the same shell session where you plan to run history -d. If mulle matching lines appear, verify the full command text and choose the correct number. For example:
history | grep 'Authorization'
might return several API calls. Delete only the entry whose command text matches the one you want removed. If the sensitive command appears more than once, each occurrence has its own history number and must be handled separately.
Delete a single command with history -d
After you have found the history number for the command you want to remove, delete that in-memory history entry with Bash’s built-in history -d command. The syntax is simple: pass the history entry number shown by history as the argument. For example, if history shows that entry 482 contains the command you want to remove, run:
history -d 482
This removes entry 482 from the current shell’s active history list. It does not delete command number 482 from every terminal you have open, and by itself it may not immediately rewrite ~/.bash_history on disk. Bash keeps a history list in memory for each running shell, then saves or appends it depending on your history settings when the shell exits or when you explicitly write history.
Example workflow
- List recent commands with numbers:
historyorhistory | tail -50. - Identify the exact entry number you want to remove, such as
482. - Delete it from the current shell history:
history -d 482. - Check that it is gone:
history | tail -50.
Be careful when deleting more than one entry by number. Once you delete an item, Bash renumbers later entries in the current history list. For example, if you delete entry 482, the command that was 483 may become 482. If you need to remove several commands, delete them from the highest history number to the lowest, or re-run history after each deletion to confirm the next number.
You can also delete the command you just ran if it contains something sensitive and is still the most recent history entry. First check the current end of the list with history | tail, then delete the matching number. In some setups, commands starting with a space may not be saved if HISTCONTROL=ignorespace or ignoreboth is enabled, but you should still verify with history rather than assuming the command was skipped.
Rank #3
Common mistakes with history -d
- Using the wrong number: The number is the Bash history entry number, not the line number from a file viewer or editor.
- Expecting immediate disk removal:
history -dupdates the current shell’s memory list. You usually still need to write the updated list back to the history file. - Forgetting other shells: Another open terminal may still have the command in its own history list and may write it back later.
- Deleting after using search: If you found the command with
history | grep, use the number displayed byhistory, then confirm before deleting.
For a one-line delete after searching, you may see examples such as history -d 482 followed by a write operation. The delete step is only the first part: it removes the selected entry from the live Bash session. To make the change survive after the terminal closes, the next step is to save the modified history list back to the history file.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Write the updated history back to disk
After you remove an entry with history -d, the deletion affects the current Bash session’s in-memory history list. That does not always mean the saved history file has been updated yet. By default, Bash writes history to the file named by HISTFILE, usually ~/.bash_history, when the shell exits. To make the change persistent immediately, write the current in-memory history list back to disk.
Use the history -w built-in command after deleting the unwanted entry:
history -d 1234
history -w
In this example, history -d 1234 removes history entry number 1234 from the current shell’s history list, and history -w overwrites the history file with the updated list. This is the usual sequence when you want to delete one command and ensure it does not remain in ~/.bash_history.
Check which history file Bash is using
Most interactive Bash shells use ~/.bash_history, but the path can be changed. Before assuming which file is being written, check HISTFILE:
Free tools Windows power users keep installed
One-click scans. No signup required.
printf '%s\n' "$HISTFILE"
If this prints nothing, the shell may not be saving history to a file. If it prints a custom path, history -w writes to that file instead of ~/.bash_history. You can also write to a specific file explicitly:
history -w ~/.bash_history
Writing to an explicit file is useful if you have changed HISTFILE temporarily or are cleaning up history for a different account environment. Be careful with the target path, because history -w replaces the file contents with the current shell’s history list.
Be careful with multiple open terminals
If you have several Bash sessions open, each one may have its own in-memory history. One terminal can rewrite ~/.bash_history after another terminal has already cleaned it. For example, you might delete an entry in Terminal A and run history -w, but later close Terminal B, which still has the old entry in memory. Depending on your shell options, Terminal B may append or write its version of history and bring the command back.
To reduce that risk, close or clean other active Bash sessions that may contain the same command. In each relevant terminal, search for the entry with history | grep 'text to find', delete the matching number with history -d, and then run history -w from the session whose history you want saved. If your setup uses shopt -s histappend, shells append history on exit instead of replacing the file, which can also preserve copies from other sessions unless those sessions are cleaned too.
Confirm the saved file no longer contains the command
After writing the updated history, verify the saved file directly. For example:
grep -n 'text to find' "$HISTFILE"
If grep returns no output, the string was not found in that history file. If it still appears, delete the relevant in-memory entry again if it is still present, or edit the file manually and remove the line. Then save the file and avoid letting another shell session rewrite the old entry back into it.
Rank #4
Remember that removing a Bash history line only affects Bash’s history mechanism and the selected history file. It does not erase terminal scrollback, shell audit logs, system accounting, process logs, backups, synchronization copies, or command output saved elsewhere. For sensitive data, rotate exposed credentials rather than relying on history cleanup alone.
Remove a command manually from .bash_history
If the command you want to remove is already saved on disk, you can edit the Bash history file directly. For most local accounts, that file is ~/.bash_history, but Bash can use a different location if the HISTFILE variable is set. Check the active path first:
printf '%s\n' "$HISTFILE"
If the output is empty, Bash will normally fall back to ~/.bash_history. Open the file with a text editor, delete only the line containing the command you want removed, save the file, and exit. For example:
nano ~/.bash_history
Inside the editor, search for a distinctive part of the command rather than scrolling through the whole file. In nano, press Ctrl+W, type part of the command, then press Enter. In vim, use /search-text. Delete the matching line, then save the file. Be careful with commands that span mulle physical lines or contain embedded newlines; remove the entire saved entry, not just the first visible fragment.
Preserve the edited file from being overwritten
Manual editing affects the history file on disk, but your current shell may still have an in-memory copy of the old history. If that shell later exits and writes its history, it can put the deleted command back into .bash_history. After editing the file, reload the cleaned history into the current shell:
history -chistory -r "$HISTFILE"
history -c clears the current shell’s in-memory history list. history -r reads the edited file back into memory. If you know HISTFILE is unset and you edited the default file, you can use history -r ~/.bash_history instead.
Use command-line tools for a targeted edit
For a one-line command, you can also remove it with tools such as sed, perl, or grep. This is useful over SSH or in scripts, but it requires precise matching so you do not delete unrelated entries. For example, to remove lines containing an exact visible token such as temporary-password-123:
grep -v 'temporary-password-123' ~/.bash_history > ~/.bash_history.tmp && mv ~/.bash_history.tmp ~/.bash_history
This keeps every line except those matching the string. If the command contains slashes, quotes, backslashes, shell variables, or regular-expression characters, prefer opening the file in an editor so you can inspect the result before saving. After any command-line edit, reload the history with history -c and history -r as shown above.
- Check the right user: edit the history file for the account that ran the command, such as
/home/alice/.bash_historyor/root/.bash_history. - Check the right shell: this section applies to Bash. Other shells use different files, such as
~/.zsh_historyfor Zsh. - Mind file permissions: do not use
sudoto edit another user’s history unless you intentionally need administrative access. - Expect timestamps on some systems: if Bash history timestamps are enabled with
HISTTIMEFORMAT, the file may include timestamp lines beginning with#. Remove the timestamp associated with the deleted command as well if you want the file to stay tidy.
Editing .bash_history only cleans Bash’s saved history file. It does not remove the command from audit logs, terminal scrollback, process accounting, backups, shell transcripts, clipboard managers, monitoring tools, or remote service logs. Treat this as a way to correct or reduce local shell history exposure, not as a complete privacy or security erasure method.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Handle multiple terminal sessions and common pitfalls
Deleting one command from Bash history is straightforward in a single terminal, but it can become confusing when several shell sessions are open at the same time. Each interactive Bash process keeps its own in-memory history list. The file, usually ~/.bash_history, is only the saved copy on disk. If you delete an entry in one terminal and then another terminal exits later, that other shell may write or append its own history and bring the command back into the history file.
Best Value
A safe approach is to clean the entry in the shell where it appears, write the cleaned history, and then check any other open terminals before closing them. In each terminal, run history | grep 'part-of-command' or inspect the surrounding history lines. If the same sensitive command exists in more than one session, remove it in each one with history -d NUMBER. After deletions, use history -w from the cleaned session to overwrite the saved file with the current in-memory history.
Common pitfalls to avoid
- History numbers change after deletion: after you run
history -d NUMBER, later entries shift up. If you need to delete several entries, re-runhistoryafter each deletion, or delete from the highest history number down to the lowest. - Other terminals can re-save old entries: a second terminal that still has the command in memory may write it back when it exits, depending on your Bash settings. Clean those sessions too, or close them carefully after clearing their history.
history -woverwrites the file: this is useful after cleanup, but it writes the current shell’s history list to disk. If that list is missing commands from other active sessions, those commands may be lost from the saved file.history -aappends instead of overwriting: appending can preserve newer commands from a session, but it will not remove a deleted line that already exists in~/.bash_history.- Manual file edits do not clean memory: if you edit
~/.bash_historywith an editor, the current shell may still have the command in memory and may write it back later.
Bash configuration can also affect the result. With histappend enabled, shells append new entries to the history file instead of replacing it at exit. This reduces accidental overwrites between terminals, but it also means a deleted command may remain in the file unless you explicitly rewrite the cleaned history or edit the file. Settings such as HISTCONTROL=ignorespace can prevent future commands from being saved if they start with a space, but they do not remove commands already recorded.
Finally, Bash history cleanup should not be treated as a complete privacy or security measure. The command may still exist in terminal scrollback, log files, audit records, process accounting, backup copies, shell sync tools, clipboard managers, or remote session logs. If the command contained a password, token, private URL, or API key, delete the history entry, then rotate or revoke the exposed secret. Removing the line from Bash history reduces casual exposure, but it does not guarantee that every copy has disappeared from the system.
Recommended Free Tools
Frequently Asked Questions
How do I delete one command from Bash history without clearing everything?
Run history to find the line number, then delete that entry with history -d NUMBER. After deleting it, run history -w to write the updated in-memory history back to your history file, usually ~/.bash_history.
Why does the command come back after I delete it with history -d?
history -d removes the entry from the current shell’s in-memory history, but it may still be in ~/.bash_history until you write the updated history with history -w. It can also reappear if another open terminal session still has the command in its own history and later writes its history to disk.
How can I find the exact history number for the command I want to remove?
Use history | grep 'part-of-command' to search for the entry, replacing the text with something unique from the command. Be aware that the grep command itself may be added to history too, so you may need to delete that search line afterward if it contains sensitive text.
Can I delete the command directly from .bash_history instead?
Yes, you can open ~/.bash_history in a text editor and remove the specific line, then save the file. This only edits the saved history file, so close other Bash sessions first or run history -c followed by history -r in the current shell to reload the edited file.
Free tools Windows power users keep installed
One-click scans. No signup required.
Does deleting a command from Bash history remove all traces of it from the system?
No, it only removes the command from Bash history. The command may still exist in terminal scrollback, system logs, audit logs, process accounting, application logs, backups, or another shell’s history file. If the command contained a password or token, rotate that secret instead of relying on history deletion alone.
Bottom Line
To delete a single Bash history entry, find its number with history, remove it with history -d NUMBER, then write the updated list back with history -w so it is removed from the saved history file. If needed, confirm by checking history again and inspecting ~/.bash_history after the session is saved.
Act quickly if the command contained sensitive data, but remember that deleting Bash history is not a complete privacy guarantee: other terminals, logs, backups, monitoring tools, or copied commands may still contain it. For the future, avoid typing secrets directly into commands and use safer options such as environment files, prompts, or secret-management tools.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →




