How to display countdown timer in bash shell script running on Linux/Unix

I want to display a countdown before purging cache from CDN network. Is there an existing command to show a conuntdown from 30..1 as 30,29,28,…1 on Linux or Unix bash shell script?

There are various ways to show a countdown in your shell scripts.

First define your message:
msg="Purging cache please wait..."
Now clear the screen and display the message at row 10 and column 5 using tput:
tput cup 10 5

Next you need to display the message:
echo -n "$msg"

Find out the length of string:
Calculate the next column:
l=$(( l+5 ))
Finally use a bash for loop to show countdown:
for i in {30..01}
tput cup 10 $l
echo -n "$i"
sleep 1

Here is a complete shell script:

# Purpose: Purge urls from Cloudflare Cache
# Author: Vivek Gite {} under GPL v2.x+
# --------------------------------------------------------
# Set me first #
countdown() {
        msg="Purging ${1}..."
        tput cup $row $col
        echo -n "$msg"
        l=$(( l+$col ))
        for i in {30..1}
                tput cup $row $l
                echo -n "$i"
                sleep 1
# Do it
for u in $urls
     curl -X DELETE "${zone_id}/purge_cache" \
     -H "X-Auth-Email: ${email_id}" \
     -H "X-Auth-Key: ${api_key}" \
     -H "Content-Type: application/json" \
     --data "{\"files\":[\"${u}\",\"${amp_url}\"]}" &>/dev/null &&  countdown "$u"

You can run it as follows:
./ url1 url2

POSIX shell version

From this post:

  set -- $*
  secs=$(( ${1#0} * 3600 + ${2#0} * 60 + ${3#0} ))
  while [ $secs -gt 0 ]
    sleep 1 &
    printf "\r%02d:%02d:%02d" $((secs/3600)) $(( (secs/60)%60)) $((secs%60))
    secs=$(( $secs - 1 ))

It can be run as follows:
countdown "00:00:10" # 10 sec
countdown "00:00:30" # 30 sec
countdown "00:01:42" # 1 min 42 sec

Sample session:

Gif 01: countdown demo

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 4 comments so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
4 comments… add one
  • Jouni "rautamiekka" Jrvinen Jun 25, 2017 @ 10:28

    How come you could escape double quotes inside double quote strings like that when it wasn’t possible before ?

    • 🐧 Vivek Gite Jun 25, 2017 @ 14:09

      You can always do that. There was no such problem:
      echo "This is a \"test\"."

      • Jouni "rautamiekka" Järvinen Jun 25, 2017 @ 14:15

        That never worked before (syntax error), had to use glueing like someone said to a question of escaping double quotes in a double quote string:

        echo "This is a "'"test"'"."

        Now it works. That was one of the stupidities of Bash, and only now they fixed it.

  • Chris Oct 6, 2017 @ 16:12

    Thanks, this post helped me out! I added some functionality so the screen does not clear… This let me see the command output from the previous loops. And I made it into an infinite looping tool which was my use case.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @