4

I accidentally put a password on the command line and needed to clear Bash shell history. That was easy enough with echo "" > ~/.bash_history.

I added the same to my .bash_logout just in case for the future. However, when I check history after a logout/logon, the last command I entered is still present in the history file. More correctly, two commands are present: the last command I entered, and the exit command from the logout.

How do I make Bash flush the command history to file so that I can clear it upon logout?

  • Strange adding this into the logout file doesn't remove the last ones. – fedorqui Oct 16 '15 at 22:18
  • Next time this happens, don't logout but kill your shell: kill -9 $$ – wurtel Oct 19 '15 at 10:23
5

Bash doesn't write its history to the history file until it exits or you explicitly invoke history -w. So if you just added your password in the current shell's history, it hasn't been written to .bash_history yet, it's just sitting there in memory. You can run fc -l to see a listing of the most recent commands, then history -d to delete an entry.

$ echo swordfish
swordfish
Oops, that was my password!
$ fc -l -2
1234 ls
1235 echo swordfish
$ history -d 1235
$ fc -l -3
1234 ls
1235 fc -l -2
1236 history -d 1236

If you've already written the history file (for example because you have history -w in PROMPT_COMMAND or a debug trap), then do this and edit .bash_history as well.

If you want to forget the current shell's history altogether, unset HISTFILE and make sure it stays unset until the shell exits. If you want to forget the current history up to this point but start recording, you can call history -c. If you want to forget all but the last 3 history entries, set HISTFILE=3 then (on a separate command line) set it back to your usual value.

2

The man page for bash provides two good solutions.....which can be put in your .*rc files

  1. export HISTFILESIZE=0

  2. export HISTSIZE=0

These can also be entered on your login shell manually as well and it will have the same effect.

  • Thanks mdpc. I visited a bash(1) man page, but I'm not clear: do the variables affect the session, or the history file? I want history for the session, I just don't want it persisted to file. – user56041 Oct 16 '15 at 22:36
  • Then you probably want HISTFILESIZE to go to zero. – mdpc Oct 16 '15 at 22:37
  • OK, thanks. HISTFILESIZE=0 worked as desired. Let me give folks time to answer the exact question I asked. If there are no relevant answers, then I'll come back to this answer. – user56041 Oct 16 '15 at 22:53
1

I had problems editing my logout too (I made a backup script that backs up my data to a server). I found this link helpful, but it is for ubuntu.

https://askubuntu.com/questions/293312/execute-a-script-upon-logout-reboot-shutdown-in-ubuntu

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy