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
  • 2,848
  • 1
  • 18
  • 21
joe28a
  • 23
  • 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 Quenot
  • 154,891
  • 35
  • 213
  • 206
  • 1
    That worked, thank you very much for your great assistance. – joe28a May 26 '20 at 00:43
  • Advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires at least 15 reputation points). If you found other answers helpful, please up-vote them. Accepting and up-voting helps future readers. Please see [the relevant help-center article](https://stackoverflow.com/help/someone-answers) – Gilles Quenot May 26 '20 at 01:22