2

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)
azbarcea
  • 3,323
  • 1
  • 20
  • 25
joe28a
  • 23
  • 1
  • 4
  • Does this answer your question? [How do I iterate over a range of numbers defined by variables in Bash?](https://stackoverflow.com/questions/169511/how-do-i-iterate-over-a-range-of-numbers-defined-by-variables-in-bash) – Shawn May 25 '20 at 23:40

1 Answers1

2

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

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223