The time
command on OSX has less features than gnu-time gtime
on Linux.
You can install gtime
on OSX with Homebrew: brew install gnu-time
Example:
> time sleep 2
real 0m2.009s
user 0m0.002s
sys 0m0.004s
> gtime sleep 2
0.00user 0.00system 0:02.00elapsed 0%CPU (0avgtext+0avgdata 1933312maxresident)k
0inputs+0outputs (0major+198minor)pagefaults 0swaps
Format output:
http://stackoverflow.com/questions/385408/get-program-execution-time-in-the-shell
# On OSx use `gtime` for time
> fmt="run { date = '$(date)', user = '$(whoami)', host = '$(hostname)', times = { user = %U, system = %S, elapsed = %e } }"
> gtime -f "$fmt" sleep 2
run { date = 'Thu Mar 13 10:52:51 EDT 2014', user = 'greg', host = 'mbair.home', times = { user = 0.00, system = 0.00, elapsed = 2.00 } }
http://mywiki.wooledge.org/BashFAQ/032 - includes redirecting of time output
This seems to be the best. The issue is that time output is not coming from standard output.
exec 3>&1 4>&2
foo=$( { time ls 1>&3 2>&4; } 2>&1 ) # Captures time only.
exec 3>&- 4>&-
echo $foo
# output:
real 0m0.016s user 0m0.003s sys 0m0.006s