While bash doesn't handle floating point numbers, the seq
utility does. [Note 1]
The basic syntax is seq FIRST INCREMENT LAST
, so in your case you could use
for count in "$(seq 10.0 0.1 20.0)"; do
# something with $count
done
If you provide two arguments, they are assumed to be FIRST and LAST, with INCREMENT being 1. If you provide only one argument, it is assumed to be LAST, with both FIRST and INCREMENT being 1. As in your example, the sequence is inclusive so both FIRST and LAST will be produced provided that INCREMENT evenly divides FIRST−LAST.
You can also include an explicit format:
$ seq -f "%06.3f" 1 .5 2
01.000
01.500
02.000
One downside of this technique is that it precomputes the entire collection of values. If the loop will execute hundreds of thousands of times, that might use up a lot of memory, in which case you could use a pipe or process substitution instead:
while read count; do
# something with count
done < <(seq 10.0 0.000001 20.0)
Notes
seq
is not Posix but it is almost always present; it's part of GNU coreutils and a similar utility, available in Mac OS X) has been in NetBSD since 3.0 and FreeBSD since 9.0.