0

I would like to set a variable (x=100) in the brackets such as

x=100    
for i in {0.."$x"..50} ;do
echo $i 
done

the desire output for i should be

0
50
100

although I get as an output

{0..100..50}
Inian
  • 12,807

2 Answers2

2

That syntax would work in zsh (where it comes from) or ksh93 or yash -o braceexpand, but not in bash where you can't use expansions within {x..y}. But here, it would be better to use ksh93-style for ((...)) loops anyway:

x=100    
for ((i = 0; i <= x; i += 50)) {
  printf '%s\n' "$i"
}
2

That simply is not possible in bash. If you see the order of expansions in bash, the variable expansion, happens at a latter time than the time the shell expands the brace {0..50}. So at the time of brace expansion, the shell sees the construct as {0..$x..50} which would be an invalid construct to expand.

You need an alternate way to this, best way would be a for loop in bash arithmetic context.

for ((i=0; i<=100; i+=50)); do 
    printf "%d\n" "$i"
done
Inian
  • 12,807