You’ve just run a long Bash command and you don’t want to type it back in. The command history allows you to find it quickly. The problem is that Bash can also store a password, an API token, or a signed URL pasted directly into the terminal.
Here’s how to read Bash history, delete a sensitive line, and prevent a secret from ending up in… ~/.bash_historyIf a true secret has already been seized, Removing it from the history is not enough : revoke it or change it.
Retrieve a command using history and Ctrl+R
To display the latest commands from the Bash session, first limit the output:
history 30
Each line is assigned a number. You can then filter the list using a non-sensitive part of the command, such as the service or tool name:
history | grep -i -- 'systemctl'
history | grep -i -- 'docker login'
Never include the password or the token itself in the search pattern: this new command would also be added to the history. If you need to delve deeper into filters, the guide dedicated to grep on Linux details case-insensitive searches and regular expressions.
In an interactive terminal, Ctrl+R launches a reverse search. Type a few characters, press Ctrl+R again to go back to an earlier occurrence, then use the right arrow to edit the line without executing it immediately.
To display the date and time associated with entries when Bash has this information:
HISTTIMEFORMAT='%F %T ' history 30
Check where Bash saves history
The visible list with history belongs to the current session. The persistent file is indicated by the variable HISTFILEBy default, Bash usually uses ~/.bash_history
printf 'HISTFILE=%sn' "${HISTFILE:-undefined}"
printf 'HISTSIZE=%sn' "${HISTSIZE:-not defined}"
printf 'HISTFILESIZE=%sn' "${HISTFILESIZE:-not defined}"
stat -- "$HISTFILE"
fi
HISTSIZE limits the number of entries kept in memory. HISTFILESIZE limits those written in the file. If HISTFILE If not defined, the session can function without a persistent file.
The history also depends on the user and the shell. Your account, root and another user are not writing to the same file. Zsh and Fish have their own mechanisms. Check the shell actually being used before cleaning up the wrong location:
printf 'Connection shell: %sn' "$SHELL"
printf 'Current process: %sn' "$0"
Remove a sensitive line without deleting the entire file
Locate the line with a pattern that does not contain the secret. Suppose that history show number one thousand eight hundred forty-two for the order in question:
history -d 1842
history -w
history -d removes the entry from the list in memory. history -w Then rewrite the file with the current list. Check the result using the command name, never the secret value:
history | grep -i -- 'command-name'
grep -n -i -- 'command-name' "$HISTFILE"
fi
Avoid editing ~/.bash_history with an editor while multiple Bash sessions are open. Another session can overwrite or add its own lines when it closes. First, close any unnecessary shells, or repeat the cleanup in each affected session, then perform a final check on the file.
The order history -c This erases the entire list in memory. It’s too abrupt to remove a single line and can cause you to lose useful history. It’s better to target the entry, write the file, and then read it back.
Why the secrecy must be revoked despite the cleaning
Deleting a Bash line is not a secure deletion. The secret could have been copied to a backup, collected by an auditing tool, synchronized to a central server, or temporarily seen in the process list if it was passed as an argument.
- Change the relevant password.
- Revoke the API token and create a new one.
- Monitor the service’s access logs.
- Check other sessions, accounts, and machines where the command may have been used.
An SSH passphrase should not be pasted directly into a command. Use an agent instead: the guide ssh-agent under Linux shows how to avoid retyping it while keeping the private key protected.
Prevent Bash from registering a sensitive command
Bash can ignore commands preceded by a space if HISTCONTROL contains ignorespaceFirst, check the active value:
printf '%sn' "$HISTCONTROL"
To ignore commands preceded by a space and consecutive duplicates in the current session:
HISTCONTROL=ignorespace:ignoredups
A command that actually begins with a space will not be saved. This setting is convenient, but it’s not a security measure: it might be absent on another machine, disabled, or bypassed by another shell.
For a one-off operation, disable history, run the command to be protected, then re-enable it:
set +o history
Now run the command without writing the secret directly in the line. Then reactivate the history:
set -o history
The best approach is to use standard input, a secrets manager, and a configuration file in mode six hundred or the identification mechanism provided by the tool. If you render HISTCONTROL permanent in ~/.bashrcSave the file and reload it only after verification. The guide on PATH and shell configuration resumes this method of cautious modification.
The manual page of bash precisely described history -d, history -w, HISTFILE, HISTCONTROL And HISTTIMEFORMATAfter cleaning, open a new session and check the file one last time: this will tell you if the secret has really disappeared from the persistent history.