Tea Timer in Bash
I grew tired of unlocking my phone and starting a timer every time I brewed tea. Running a simple shell script is far more convenient, and writing it also gave me a chance to refresh my dulled shell scripting skills.
Here is the full script which I named tea in my file system:
#!/bin/bash
NOTIFICATION_SOUND=~/.scripts/notification.mp3
DELAY="${1:-180}"
DELAY_REMAINING=$DELAY
echo "Timer set to $DELAY seconds"
echo "Remaining: $DELAY_REMAINING"
while [ "$DELAY_REMAINING" -ne 0 ]; do
sleep 1
((DELAY_REMAINING--))
echo -e "\e[1A\e[KRemaining: $DELAY_REMAINING"
done
notify-send 'Tea is Ready' "Timer was set to $DELAY seconds"
pw-play "$NOTIFICATION_SOUND" &
So how does it work? First, we set some variables:
NOTIFICATION_SOUND=~/.scripts/notification.mp3
DELAY="${1:-180}"
NOTIFICATION_SOUND contains the path for a sound which gets played after the timer runs out. DELAY sets the delay to $1, or to default of 180 seconds if the $1 parameter is missing.
Next interesting part is the timing itself. It runs in a loop until DELAY_REMAINING reaches zero. The loop also contains a peculiar echo:
echo -e "\e[1A\e[KRemaining: $DELAY_REMAINING"
This basically replaces the last line in stdout, where \e[1A moves the cursor to the previous line and \e[K erases the line, therefore letting us overwrite the line with new content.
When the timer runs out, system notification is sent with notify-send and the predefined sound is played trough pipewire using pw-play:
notify-send 'Tea is Ready' "Timer was set to $DELAY seconds"
pw-play "$NOTIFICATION_SOUND" &
The usage is simple: running tea with an optional parameter, such as tea 120.