Time a command with milliseconds
Time a command to the millisecond in Bash 5 with EPOCHREALTIME, which holds the epoch time with microseconds and needs no date or bc process.
time reports seconds with a fixed format, and you want a number of milliseconds you can compare in a script.
start=$EPOCHREALTIME
sleep 0.2
end=$EPOCHREALTIME
echo "start=$start"
echo "elapsed: $(( (${end/./} - ${start/./}) / 1000 )) ms"start=1793973180.212441
elapsed: 200 msBash 5.0 added EPOCHREALTIME, which expands to the seconds since the epoch with a six digit fraction, as listed among the shell variables. Deleting the dot with ${var/./} turns each reading into microseconds, an integer that shell arithmetic can subtract, and dividing by 1000 gives milliseconds. Nothing is forked, so the measurement does not add its own cost.
The exact figure changes on every run, and sleep 0.2 can report a few milliseconds above 200. The locale can change the decimal separator in EPOCHREALTIME, so a comma in the output breaks the substitution. Set LC_NUMERIC=C in that case. It needs Bash 5.0 or later, not the 3.2 that macOS ships.