Is it possible to have your bash history save the total execution time of every command you run?

i.e. as if every command were prefaced with 'time' and the result was stored in the history along with the command?

clarification: I already know that you can add a timestamp of when the command was started - but I would like to know the total execution time, not just when it was started.

  • not a duplicate - see clarification Commented Dec 5, 2016 at 15:27
  • 1
    Ah, OK, I'd understood "actual execution time" as "time at which the command was executed". Commented Dec 5, 2016 at 15:30
  • Possible duplicate of Bash history with timestamps
    – techraf
    Commented Dec 5, 2016 at 20:08
  • 1
    @tecraf - not a duplicate - see clarification Commented Dec 5, 2016 at 20:19

2 Answers 2


By defining HISTTIMEFORMAT in your startup scripts, you will have a timestamp added to every line of your bash history file. The value of this variable is the way (*) timestamps must be printed on the screen. Here is mine :


(*) See man 3 strftime (if you don't have it, man date should do the trick)

  • 2
    yes, that will tell you what time you ran the command - but not the total execution time of it. Commented Dec 5, 2016 at 15:26
  • 1
    @slashdottir Sorry, I must have misread your question. I don't know of any such feature.
    – xhienne
    Commented Dec 5, 2016 at 15:29

Child process usage is set somewhere within the wait3 or wait4 system calls via a struct rusage pointer (wait4(2)). Under the bash sources, we find:

$ egrep -r 'wait[34][[:blank:]]*\(' .
./jobs.c:   wait3 ((union wait *)statusp, options, (struct rusage *)0)
./jobs.c:   wait3 (statusp, options, (struct rusage *)0)
./jobs.c:   wait3 (statusp, options, (int *)0)

The blank (struct rusage *)0 indicates that the resource usage will not be returned to bash. A struct rusage search does turn up time_command function in execute_cmd.c, and this does appear to collect usage information via getrusage(2). However, this is for the TIMEFORMAT of the time builtin, and does not appear to intersect with the history code. Without patching bash, I guess you'd have to do some sort of kluge involving running everything under time and then capturing the standard error the time information appears on to some file?

  • I'm not above a good kluge ... :) Commented Dec 5, 2016 at 17:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .