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?
SECONDS
variable - see for example Time elapsed of a bash script (days/minutes/seconds) – steeldriver Oct 28 '22 at 20:53time
command does – Qwerty-uiop Oct 28 '22 at 20:56time
. It's a shell built in and doesn't conform to normal redirection and capture of stdin and stdout. You should consider a wrapper script that grabs a timestamp fromdate
before and after the script runs. – brunson Oct 28 '22 at 21:05