Bash countdown timer

Using a bit of Bash scripting, it’s possible to create a countdown timer in Linux. This will allow you to countdown in seconds, minutes, or to some future date.

Check out the script below. You can copy it to your own system, then try some of our examples to see how the syntax works.

In this tutorial you will learn:

  • How to use a Bash countdown timer on Linux
Bash countdown timer
Bash countdown timer
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash countdown script
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Bash countdown script




The countdown script is below. To use it, just paste the code into a new file. Bash scripts are quite easy to understand and edit, so feel free to make any changes to it that you find necessary. It’s also hosted on a dedicated Git repository.

#!/bin/bash 
 
if [ "$#" -lt "2" ] ; then 
	echo "Incorrect usage ! Example:" 
	echo './countdown.sh -d  "Jun 10 2011 16:06"' 
	echo 'or' 
	echo './countdown.sh -m  90' 
	exit 1 
fi 
 
now=`date +%s` 
 
if [ "$1" = "-d" ] ; then 
	until=`date -d "$2" +%s` 
	sec_rem=`expr $until - $now` 
	echo "-d" 
	if [ $sec_rem -lt 1 ]; then 
		echo "$2 is already history !" 
	fi 
fi 
 
if [ "$1" = "-m" ] ; then 
	until=`expr 60 \* $2` 
	until=`expr $until + $now` 
	sec_rem=`expr $until - $now` 
	echo "-m" 
	if [ $sec_rem -lt 1 ]; then 
		echo "$2 is already history !" 
	fi 
fi 
 
_R=0
_C=7
tmp=0
percent=0
total_time=0
col=`tput cols`
col=$[ $col -5 ]

while [ $sec_rem -gt 0 ]; do 
	clear 
	date 
	let sec_rem=$sec_rem-1 
	interval=$sec_rem 
	seconds=`expr $interval % 60` 
	interval=`expr $interval - $seconds` 
	minutes=`expr $interval % 3600 / 60` 
	interval=`expr $interval - $minutes` 
	hours=`expr $interval % 86400 / 3600` 
	interval=`expr $interval - $hours` 
	days=`expr $interval % 604800 / 86400` 
	interval=`expr $interval - $hours` 
	weeks=`expr $interval / 604800` 
	echo "----------------------------" 
	echo "Seconds: " $seconds 
	echo "Minutes: " $minutes 
	echo "Hours:   " $hours 
	echo "Days:    " $days 
	echo "Weeks:   " $weeks 

	echo -n "["

	progress=$[$progress + 1]
	if [ $total_time -lt 1 ] ; then
		total_time=$[$hours * 3600 + $minutes * 60 + $seconds]
	fi
	
	printf -v f "%$(echo $_R)s>" ; printf "%s\n" "${f// /=}"
	_C=7
	tput cup 7 $col

	tmp=$percent
	percent=$[$progress * 100 / $total_time]
	printf "]%d%%" $percent
	change=$[$percent - $tmp]

	_R=$[ $col * $percent / 100 ]

	sleep 1
done
printf "\n"

After you’ve saved the script into a file, you’ll need to give it execute file permissions. For example, if you named your script countdown.sh:

$ chmod +x countdown.sh


Bash countdown script usage examples

Now that your script is saved and executable, check out some of the examples below to learn how to use it.

Using the Bash countdown timer script on Linux
Using the Bash countdown timer script on Linux
  1. Countdown time to 90 minutes from now. The -m option indicates minutes.
    $ ./countdown.sh -m 90
    
  2. Countdown time to a future date, such as March 23rd, 2036. The -d option indicates a date.
    $ ./countdown.sh -d "Mar 23 2036"
    
  3. Countdown to a future time, such as 21:06.
    $ ./countdown.sh -d 21:06
    
  4. You can also specify a future time right down to the exact seconds:
    $ ./countdown.sh -d 21:06:45
    

Closing Thoughts

Although Linux doesn’t have a default command to generate a countdown timer, Bash allows for so much flexibility that we can code one ourselves. A much simpler one can be used, but the one above has been made more robust to handle a variety of situations.



Comments and Discussions
Linux Forum