0

I would like to measure time spent on a script and save it to a variable. This is my current code:

time_spent=$(time ./script.sh)
...
echo $time_spent

Here is how it should work: The script output should go into the console immediately since the script may need user interaction. The time spent should be saved to a variable for later use.

Can this be done with time or should I use something else?

1 Answers1

1

/usr/bin/time rather than the time builtin can write the times to a file with the -o option.

$ /usr/bin/time -p -o times sleep 1
$ cat times
real 1.01
user 0.00
sys 0.00

See the man page for more info.

brunson
  • 179