1

I'm new to bash scripting. I'm trying to use variables for counters in a nested for loop, like this:

dir_count=$(find dump_${d}/* -maxdepth 0 -type d | wc -l)
count_by=11

for ((i=0;i<=$dir_count;i+=$count_by))
do
    ((start=$i+1))
    ((end=$count_by+$i))
    echo  $start $end
    for dir in {$start..$end}
        echo $dir
    done
done

Output is this (I'm getting errors):

1 11
./loopy.sh: line 23: [: {1..11}: integer expression expected
12 22
./loopy.sh: line 23: [: {12..22}: integer expression expected
23 33
./loopy.sh: line 23: [: {23..33}: integer expression expected

1, 11, 12, 22, 23, 33 all look like integers to me! Is it possible to type variables? I thought it wasn't.

Thanks!

1 Answer 1

1

In bash, brace-expansion will happen much before variable-expansion, so your code

for dir in {$start..$end}

will never do what it is supposed to do; use a proper loop in bash with a C-style for-loop as

for ((dir=start; dir<=end; dir++)); do
    echo "$dir"
done

Quoting from the man bash page,

[..] Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly textual [..]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.