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

3 Responses to “Eternal Bash history”

  1. Andy Teijelo Says:

    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.

    • edin1 Says:

      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:

      “It is tempting, if the only tool you have is a hammer, to treat everything as if it were a nail.”


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.