How to Create a Timer in Bash

· by Victor Mendonça · Read in about 1 min · (79 words) ·

Following my previous post on «How to Create a Prompt With Timeout in Bash », we will now see how to create a timer (countdown) in Bash using the built-in $SECONDS variable.

The $SECONDS variable holds in the number of seconds the script has been running. So it can easily be used to create a timer inside your script in Bash.

$ bash -c 'while true ; do echo -en "\r${SECONDS}s elapsed" ; sleep 1 ; done'
203s elapsed

Imgur

code with