How to loop through a range of decimal numbers in bash?

12.3k views Asked by At

I'd like to generate a sequence of equally spaced decimal numbers.

For example, I want to echo all numbers between 3.0 and 4.5, with step 0.1. I tried $ for i {3.0..4.5..0.1}; do echo $i; done, but this gives an error.

I also tried $ for i in $(seq 3.0 4.5 0.1); do echo $i; done but nothing happens.

3

There are 3 answers

1
Ole Tange On BEST ANSWER

I also tried $ for i in $(seq 3.0 4.5 0.1); do echo $i; done but nothing happens.

The order is wrong:

$ for i in $(seq 3.0 0.1 4.5); do echo $i; done
5
bmscomp On
 for i in {3.0,4.5,0.1}; do echo $i; done
3
drpetermolnar On

If you're looking for a loop from 3.5 to 4.5 in 0.1 steps this would work

for x in {35..45}; do
     y=`bc <<< "scale=1; $x/10"`
     echo $y
done

The same with 0.01 steps

for x in {350..450}; do
         y=`bc <<< "scale=2; $x/100"`
         echo $y
done