Reputation: 922
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!
Upvotes: 1
Views: 48
Reputation: 85580
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 [..]
Upvotes: 1