Date and Time Functions in Bash

Background

Bash is very powerful for handling dates and times, but you need to get to grips with it’s syntax. This article contains a few commonly used date and time functions that one could use when doing Bash scripts.

How to determine the time elapsed in Bash

START_TIME=$(date +%s)
echo -n Finding mp3 files
sleep 3 # some time consuming command
echo .\ Elapsed time: $(( $(date +%s)-START_TIME ))

How to echo the date in Bash

echo "Today is $(date)"

Sleep Times Reference

sleep .5 # Waits 0.5 second.
sleep 5 # Waits 5 seconds.
sleep 5s # Waits 5 seconds.
sleep 5m # Waits 5 minutes.
sleep 5h # Waits 5 hours.
sleep 5d # Waits 5 days.

References

https://www.cyberciti.biz/faq/bash-shell-script-how-to-add-todays-date/
https://unix.stackexchange.com/questions/305325/calculate-time-elapsed-taken-by-each-command-in-real-time-like-a-stopwatch
https://stackoverflow.com/questions/21620406/how-do-i-pause-my-shell-script-for-a-second-before-continuing
https://stackoverflow.com/questions/8903239/how-to-calculate-time-elapsed-in-bash-script
https://en.wikipedia.org/wiki/Bash_(Unix_shell)

 

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top