brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: history -d removes a password you typed from bash history

Bash
~/tips/tip-bash-history-d-delete-entry

history -d 1042

Delete one line, or a range, from your history after typing something secret.

Typing a password straight into a command, like mysql -p'secret', happens. That line now sits in your history and, when the shell closes, in ~/.bash_history.

Find its number, then delete that entry:

history | tail -5
#  1041  ls
#  1042  mysql -u root -p'secret'
#  1043  history | tail -5
history -d 1042

Bash 5 also takes a range, like history -d 1040-1042, and negative numbers that count from the end. With negative numbers the history -d command itself counts as the newest line, so I find a plain positive number from the list safer.

This changes the list in memory. If the session already wrote its history to the file, for example with history -a in your prompt, the line is in ~/.bash_history as well. Rewrite the file from memory with history -w, or edit the file in a text editor.

Deleting it from history does not undo the leak. The password may also have been visible in ps output while the command ran, so change it if other people use that machine. For next time, a space at the start of a command with HISTCONTROL=ignorespace keeps it out of history from the start.

Further reading: Bash manual: Bash History Builtins

More Bash tips echo {01..10} cmd > file 2>&1 export HISTIGNORE='ls*:pwd:cd*' #rsync -av --delete /srv/media/ /mnt/backup/media/ APP_HOST=example.com All Bash tips (52) All topics

Link