Eternal Bash history
March 22, 2010
After reading “Bash eternal history” (I think I picked it up via FriendFeed) I put the following lines in my .bashrc file:
export HISTTIMEFORMAT="%s "
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND ; }"'echo $$ $USER "$(history 1)"|python -c "import sys,time;cli = sys.stdin.read().split(None,4);print \" \".join([cli[0], cli[1], cli[2], time.strftime(\"%Y-%m-%d %H:%M:%S\", time.localtime()), cli[4]])," >> ~/.bash_eternal_history'
The Python bits I added put a human readable timestamp in the file, instead of the standard UNIX timestamp.
Watch out of putting your passwords on the command line!
Advertisement
September 9, 2010 at 7:07 pm
Actually, you can achieve the same effect in a simpler way, by just changing the value of HISTTIMEFORMAT in the first line, like this:
export HISTTIMEFORMAT=”%Y-%m-%d %H:%M:%S ”
PROMPT_COMMAND=”${PROMPT_COMMAND:+$PROMPT_COMMAND ; }”‘echo $$ $USER “$(history 1)” >> ~/.bash_eternal_history’
It also has the advantage of not spawning a new process every time you hit Enter. I would also recommend to keep “%s” somewhere in HISTTIMEFORMAT. That would help distinguished repeated or missed timestamps during daylight time saving transitions.
September 10, 2010 at 4:58 am
Thanks for the (original) article and this tip!
It turns out there’s an even shorter solution:
export HISTTIMEFORMAT="%F %T "As for me complicating things by using Python:
September 10, 2010 at 4:10 pm
It happens to me too!
Python is absolutely amazing.