sometimes it is usefull to meassure how long what took in the shell

vim /scripts/stopwatch.sh

#!/bin/bash
# function to get current time in seconds and milliseconds
get_time() {
    date +%s%N | cut -b1-13
}

# initial time
start_time=$(get_time)

echo "press [ctrl+c] to stop..."

# loop to continuously display elapsed time
while true; do
    current_time=$(get_time)
    elapsed_time=$((current_time - start_time))
    
    # convert elapsed time to seconds and milliseconds
    seconds=$((elapsed_time / 1000))
    milliseconds=$((elapsed_time % 1000))
    
    # format the output
    printf "\rTime elapsed: %02d.%03d seconds" "$seconds" "$milliseconds"
    sleep 0.1  # Update every 100ms to reduce CPU usage
done

liked this article?

  • only together we can create a truly free world
  • plz support dwaves to keep it up & running!
  • (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
  • really really hate advertisement
  • contribute: whenever a solution was found, blog about it for others to find!
  • talk about, recommend & link to this blog and articles
  • thanks to all who contribute!
admin