In order to find out how long certain operations within a Bash (v4+) script take, I would like to parse the output from the time
command "separately" and (ultimately) capture it within a Bash variable (let VARNAME=...
).
Now, I am using time -f '%e' ...
(or rather command time -f '%e' ...
because of the Bash built-in), but since I already redirect the output of the executed command I'm really lost as to how I would go about to capture the output of the time
command. Basically the problem here is to separate the output of time
from the output of the executed command(s).
What I want is the functionality of counting the amount of time in seconds (integers) between starting a command and its completion. It doesn't have to be the time
command or the respective built-in.
Edit: given the two useful answers below, I wanted to add two clarifications.
- I do not want to throw away the output of the executed command, but it will not really matter whether it ends up on stdout or stderr.
- I would prefer a direct approach over an indirect one (i.e. catching output directly as opposed to store it in intermediate files).
The solution using date
so far comes closes to what I want.
fork()
,execvp()
andwait3()/wait4()
. This is ultimately what time and friends are doing. I'm not aware of a simle way to do it in bash/perl without redirecting to a file or similar approach.