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

You can create a countdown timer in a bash shell script using the for loop and the sleep command. Here’s an example that counts down from 10 seconds:

#!/bin/bash

echo "Starting countdown..."
for i in {10..1}; do
echo -n "$i "
sleep 1
done
echo "Time's up!"

The script will display the countdown in the terminal, with each iteration of the loop printing the current second and waiting for one second before continuing.

Note: The echo -n option is used to print the output without a newline, so the seconds are displayed on the same line.

Note: The sleep command can be used to wait for any number of seconds, so you can adjust the countdown timer as needed.

Leave a Comment