User1291
User1291

Reputation: 8182

bash - brace expansion not expanding?

On Kubuntu 15.10

echo $BASH_VERSION
4.3.42(1)-release

I try

reps=1
threads={1}{10..60..10}

for((r=0;r<$reps;r++))
do
    tCount=0
    for t in $threads
    do
        echo "t=$t, tCount=${tCount}"
        #do something funny with it
        ((tCount++))
    done
done

and it produces a single line

t={1}{10..60..10}, tCount=0

How do I get this to work?

edit

I expect

t=1, tCount=0
t=10, tCount=1
t=20, tCount=2
t=30, tCount=3
t=40, tCount=4
t=50, tCount=5
t=60, tCount=6

update

note that threads=({1}{10..60..10})

and then for t in ${threads[@]}

will prefix the 10..60..10 range with the string {1}

(i.e. {1}10,{1}20,..,{1}60 )

Upvotes: 1

Views: 688

Answers (1)

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21492

The {1} expression is just a string, since it doesn't conform to the brace expansion syntax:

A sequence expression takes the form {X..Y[..INCR]}, where X and Y are either integers or single characters, and INCR, an optional increment, is an integer.

The brace expansion is performed before variable expansion, so you can't expand braces by just referring to a variable:

The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion); word splitting; and filename expansion.

Whether write the brace expansion directly, or use eval (usually not recommended).

Example:

tCount=0
for t in {1,{10..60..10}}; do
  echo "t=$t tCount=$tCount"
  (( tCount++ ))
done

Upvotes: 4

Related Questions