1

I've just started shell scripting. Here is my code:

read totalNumbers

for i in {1..$totalNumbers..1}
do
  echo "Welcome $i times"   
done

When the input is given as 100, I'm getting the output as

Welcome {1..100..1} times

But I need something like

Welcome 1 times

Welcome 2 times

Welcome 3 times

.

.

.

Welcome 100 times
NirAv JaIn
  • 1,035
  • 1
  • 9
  • 16

2 Answers2

2

You cannot use variable names inside {..} in shell.

You can use ((..)) for arithmetic in BASH:

for (( i=1; i <= $totalNumbers; i++ )); do
  echo "Welcome $i times"   
done

Even for (( i=1; i <= totalNumbers; i++ )) would work.

anubhava
  • 761,203
  • 64
  • 569
  • 643
1

If you want to use

{1..$totalNumbers..1}

anyway, replace it by

$(eval echo {1..$totalNumbers..1})

$(...) runs a subshell.

Cyrus
  • 84,225
  • 14
  • 89
  • 153