#!/bin/bash
# Define a timestamp function
timestamp() {
date +"%T" # current time
}
# do something...
timestamp # print timestamp
# do something else...
timestamp # print another timestamp
# continue...
timestamp() {
IFS=. read S US <<<$EPOCHREALTIME # Read epoch seconds/microseconds
MS=$((10#$US/1000)) # Convert to milliseconds (interpret in base-10, even with leading 0)
printf '%(%F %T)T.%03i' $S $MS # Emit formatted timestamp
}
TS=$(timestamp) # Invoke function, assign to variable