Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to keep timestamps on the commands logged in my Bash $HISTFILE, is it possible?

I did not manage to set it up using man bash as an information source.

My other options are as follows:

function thebanana() {
  local -r -a bash_commands=(
    # ... more coconut commands
  for bash_command in "${bash_commands[@]}"; do
    printf "${bash_command}"
    printf ":"
export HISTFILE=banana
export HISTIGNORE="$(thebanana)"
export HISTSIZE=999999
export HISTFILESIZE=999999999
export HISTCONTROL=ignoredups:erasedups

I should have mentioned I am on OS X Mountain Lion (sigh). uname -a gives me:

Darwin CoconutMac.local 12.2.0 Darwin Kernel Version 12.2.0: Sat Aug 25 00:48:52 PDT 2012; root:xnu-2050.18.24~1/RELEASE_X86_64 x86_64

and echo $BASH_VERSION gives me:


Tried adding this:

export HISTTIMEFORMAT='%b %d %I:%M:%S %p '

and it only prefixes this kind of timestamps to commands:


I try echoing back the variable (echo $HISTTIMEFORMAT), it has the right value.


I even removed .profile completely to debug this. Still only funny timestamps:


I don't know how to further troubleshoot this... :(

Solution: I was using a script that reads the $HISTFILE directly, not the history built-in so the epoch-based timestamp (secs since Coordinated Universal Time (UTC) of January 1, 1970) was not being translated using the date formatting string. Plain-old history works fine, I'll use that instead.

share|improve this question
Have you tried setting HISTTIMEFORMAT? – jw013 Oct 1 '12 at 1:36
Probably I am getting the strftime format wrong. It would help if the man page included at least a couple of examples.. +%Y-%m-%d %H-%M-%S ? – Robottinosino Oct 1 '12 at 1:43
up vote 12 down vote accepted

Yes, put this in ~/.bashrc :


Then, run the following commands :

. ~/.bashrc

It will look like this :

 5200  2012-09-30 23:55:37 find -printf '%Ts %f\n'
 5201  2012-10-01 00:00:58 ls
 5202  2012-10-01 00:03:45 cd

Explanations of the output :

  • first col is the unique id
  • second one is the date, third is the hour
  • latest is your command line
share|improve this answer
Nope, doesn't work on OSX.. :( Prob that's the issue. A strange OS. – Robottinosino Oct 1 '12 at 1:58
Did you source ~/.bashrc ? – Gilles Quenot Oct 1 '12 at 2:05
I most definitely did. It is doing "something": adding a commented line before the command with a numeric timestamp: #1349057149 – Robottinosino Oct 1 '12 at 2:07
The only ressource I've found accordingly to this is apple.stackexchange.com/questions/33970/… (search for HISTTIMEFORMAT) – Gilles Quenot Oct 1 '12 at 2:13
Copied that HISTTIMEFORMAT='%b %d %I:%M:%S %p ' verbatim, still no luck. If I echo $HISTTIMEFORMAT it's the right value. This is interesting! – Robottinosino Oct 1 '12 at 2:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.