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\""