Using time on bash functions (not commands)
Use the time
keyword instead of the external command. Using the keyword allows you to run time
on any shell command, including function calls, not just on running a program. You can control the output format to some extent through the TIMEFORMAT
variable.
TIMEFORMAT=%2U
time run_eclipse_on …
echo "$i::$stat"
The time
output gets printed on its own line, though. Bash allows a trick: you can change TIMEFORMAT
during the command, so you can stuff more things in there.
time { run_eclipse_on …; TIMEFORMAT="${i//%/%%}::${stat//%/%%}::%2U"; }
The output from time
is printed to standard error. If you need it on standard output, just redirect with 2>&1
. That will also redirect whatever the command printed on stderr, however. To preserve stderr, you can do some file descriptor shuffling.
{ time { {
run_eclipse_on …;
TIMEFORMAT=$stat::%2U;
} 2>&3; } 2>&1; } 3>&2