Skip to content

Instantly share code, notes, and snippets.

@rawaludin
Created March 11, 2015 14:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rawaludin/9c24bf900d798d66ce29 to your computer and use it in GitHub Desktop.
Save rawaludin/9c24bf900d798d66ce29 to your computer and use it in GitHub Desktop.
simple stopwatch in bash
#!/bin/bash
function st() {
BEGIN=$(date +%s)
if [ -z "$1" ]; then
echo Starting Stopwatch...
else
echo $@...
fi
while true; do
NOW=$(date +%s)
let DIFF=$(($NOW - $BEGIN))
let MINS=$(($DIFF / 60 % 60))
let SECS=$(($DIFF % 60))
let HOURS=$(($DIFF / 3600 % 24))
let DAYS=$(($DIFF / 86400))
# \r is a "carriage return" - returns cursor to start of line
printf "\e[36m\r%3d Days, %02d:%02d:%02d" $DAYS $HOURS $MINS $SECS
sleep 0.25
done
}
@nonopolarity
Copy link

nonopolarity commented Jul 30, 2017

isn't this going to contaminate the globals by introducing NOW, DIFF, MINS, etc? How about using local NOW=$(date +%s) etc?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment