Press "Enter" to skip to content

Countdown timer w/spinner

Ever wonder how they program the little spinner and such into a CLI script? ┬áHere is an example i ran across somewhere… Modify it to your liking.

[bash]
#!/bin/bash

TIMER=60
DIVISOR=1
COUNTER=${TIMER}
C=0

while [ $COUNTER -gt 0 ]
do
case "$(($C % 4))" in
0) char="/"
;;
1) char="-"
;;
2) char="\"
;;
3) char="|"
;;
esac
echo -ne "${char} — ${COUNTER} r"
COUNTER=$[$COUNTER-$DIVISOR]
C=$[$C+1]
sleep $DIVISOR
done
echo -ne ‘n’

[/bash]