To display a countdown timer in a bash shell script running on Linux or Unix, you can use the sleep
and echo
commands in a loop.
Here is an example of a bash shell script that displays a countdown timer for 10 seconds:
#!/bin/bash # Set the countdown timer to 10 seconds countdown=10 # Loop until the countdown timer reaches 0 while [ $countdown -gt 0 ] do # Print the countdown timer to the screen echo "Countdown timer: $countdown seconds" # Decrement the countdown timer by 1 second countdown=$((countdown-1)) # Sleep for 1 second sleep 1 done # Print a message when the countdown timer reaches 0 echo "Countdown timer expired!"
This script will loop until the countdown timer reaches 0, displaying the countdown timer and sleeping for 1 second between iterations. When the countdown timer reaches 0, the script will print a message indicating that the countdown timer has expired.
You can adjust the countdown timer and the sleep interval to suit your needs. For example, to set the countdown timer to 30 seconds and sleep for 5 seconds between iterations, you can use the following script:
#!/bin/bash # Set the countdown timer to 30 seconds countdown=30 # Loop until the countdown timer reaches 0 while [ $countdown -gt 0 ] do # Print the countdown timer to the screen echo "Countdown timer: $countdown seconds" # Decrement the countdown timer by 1 second countdown=$((countdown-1)) # Sleep for 5 seconds sleep 5 done # Print a message when the countdown timer reaches 0 echo "Countdown timer expired!"