1
0
Files
em.0x45.cz/content/posts/tea-timer-in-bash/index.md
Emil Miler e9b9372dac
All checks were successful
Build / build (push) Successful in 20s
Tea Timer in Bash
2025-10-29 12:45:31 +01:00

1.8 KiB

+++ title = "Tea Timer in Bash" date = 2025-10-29

[taxonomies] categories = ["Linux"]

[extra] author = "Emil Miler" +++

I grew tired of unlocking my phone, opening and starting a timer when brewing tea, whereas running a simple shell script is much more convenient, at least for me. Writing this script also helped me 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.