Eternal BASH History

Have you ever needed some command that you worked so hard for it
but it has being too long ago that it is already gone from Bash history ?
a simple solution is to use PROMPT_COMMAND Bash parameter .
how it works : PROMPT_COMMAND will execute the value as a command prior to issuing each primary prompt.

so if we set something like “history 1 >>~/.myhistory” , bash will write
the last history line to ~/.myhistory . nice ha
so in other words what can i do with that ?
well you can keep track of all commands , but there is a security issue
that needs to be taken , because this file saves all your history command ,
if you enter some passwords like running “mysql –password=blabla …”
it would be saved . so the first step would be to set permissions over the file
so only you can read it ( 0600 )

~$ chmod 600 ~/.myhistory

now lets put it all together by adding the line to your .bashrc

PROMPT_COMMAND='history 1 >> ~/.myhistory'

now when ever you login , the file will hold all your history ,
but now you see that every you press will write as the last command
as duplicate lines . the way i found to go around this is to simply remove duplicate
lines at login/logout . add this lines to ~/.bash_logout or ~/.bashrc

cat .myhistory |uniq >.myhistory2
mv -f .myhistory2 .myhistory 
Tagged with: