Using an input variable in a 'For Loop' in Bash

Viewed 256

I am trying to use the #REPLY input variable in the following for loop code below;

If I use syntax such as for i in {60..0} then it works fine however; trying the input variable from the read statement "for i in {$REPLY..0}" it fails. It appears that the read does not recognize the input as an integer value; can someone assist please?, thank you.

#!/bin/bash
clear
tput cup 5
read -p "What is the number of seconds? "
echo "Started:" $(date)
echo " "
echo " "
echo "Countdown Timer"
echo " "

for i in {$REPLY..0}
do
  tput cup 10 $1
  printf '%dh:%dm:%ds\n' $(($i/3600)) $(($i%3600/60)) $(($i%60))
  tput civis
  sleep 1
done
tput cnorm
echo " "
echo "Finished:" $(date)
1 Answers

Yes, you can't use a variable in brace expansion. So, the solution is simple arithmetic form loop :

for ((i=REPLY; i>=0; i--)); do
    [...]

...and please, no responses with the evil eval, thanks

Related