This Bash script creates a countdown timer that either accepts the duration in seconds as a command-line argument or prompts the user for input. It displays the remaining time in the Terminal and sends a native macOS notification when the countdown completes.
Click to view script…
#!/bin/bash
# Simple Countdown Timer for macOS with Interactive Input
# Check if a time (in seconds) was provided as an argument
if [ $# -eq 0 ]; then
# Prompt the user for input if no argument is given
read -p "Enter number of seconds for the countdown: " TIME_LEFT
else
TIME_LEFT=$1
fi
# Validate that the input is a positive integer
if ! [[ $TIME_LEFT =~ ^[0-9]+$ ]] ; then
echo "Error: Please enter a positive integer for seconds." >&2
exit 1
fi
# Countdown loop
while [ $TIME_LEFT -gt 0 ]; do
echo -ne "Time left: ${TIME_LEFT} second(s) \r"
sleep 1
((TIME_LEFT--))
done
# Move to a new line after the countdown
echo -e "\nTime's up!"
# Display a macOS notification using osascript
osascript -e 'display notification "Countdown Complete!" with title "Timer"'