Skip to content

Instantly share code, notes, and snippets.

@QasimTalkin
Last active June 26, 2023 13:05
Show Gist options
  • Select an option

  • Save QasimTalkin/c9de47eb8dc6c8b87cf4f8d6e65f5e52 to your computer and use it in GitHub Desktop.

Select an option

Save QasimTalkin/c9de47eb8dc6c8b87cf4f8d6e65f5e52 to your computer and use it in GitHub Desktop.
This is a Bash function called `timer` that starts a countdown timer with an optional duration

Shell Timer Function

This guide explains how to create a timer function in the shell/iterm/Terminal using Bash scripting. The function allows you to start a countdown timer with an optional duration. When the timer ends, it plays a sound and announces the end of the timer.

Installation

To use the timer function, follow these steps:

  1. Ensure you have Homebrew installed on your macOS.
  2. Open the terminal and run the following command to install the required tool:
    brew install drgrib/tap/ttimer
# timer - Starts a countdown timer with an optional duration. When the timer ends, it plays a sound and announces the end of the timer.
# Usage: timer [duration]
# Note: This function requires the 'ttimer' command. If you don't have it installed, you can install it using Homebrew by running the following command:
# brew install drgrib/tap/ttimer
# Arguments:
# duration (optional): The duration of the countdown timer. If not provided, a default duration of 2 hours (2h) will be used.
# Example usage:
# timer # Starts a timer with the default duration of 2 hours.
# timer 1h30m # Starts a timer with a duration of 1 hour and 30 minutes.
function timer() {
local duration
if [[ -n "$1" ]]; then
duration="$1"
else
duration='2h'
fi
timer_output=$(ttimer "$duration" -q)
osascript -e 'do shell script "afplay /System/Library/Sounds/Glass.aiff"'
say "Countdown Timer for \"$duration\" ended"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment