Simple CLI Timer
Ever wanted to run a super simple timer directly from the command line? Oh, you havenβt? Well, you can still read this anyway since you might learn something π.
I built a super basic implementation with a for loop and sleep
and while
technically this timer isnβt really accurate with when it completes, for
simple timers, it getβs the job done.
#!/bin/bash
β
# Check if the user has input a duration for the timer
if [ -z "$1" ]; then
echo "Usage: $0 <minutes> [message]"
exit 1
fi
β
# Timer duration
DURATION="$1"
β
# Optional custom message
MESSAGE=${2:-"Timer done!"}
β
# Start the timer
for i in $(seq 1 "$DURATION"); do
REMAINING=$((DURATION - i + 1))
echo -ne "\r${REMAINING}m "
sleep 60
done
echo -e "\rTime is up!"
β
# Show the notification
osascript -e "display notification \"$MESSAGE\" with title \"Time is Up!\" sound name \"Submarine\""