Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How to generate a numbers

0.01, 0.02, 0.03 ... until 0.2

in bash ?

i run this example but it doesn't work

#!/bin/bash
#
# Script for running several experimentations one the same JVM
#

for ((i = 0; i < 0.1; i += 0.01))
do
echo $i
done 
share|improve this question
1  
bash only has integer arithmetic –  glenn jackman May 15 '12 at 17:19

3 Answers 3

Simple and easy:

seq BEGIN STEP END

example:

seq 0.0 0.1 2.0

or (default +1)

for i in `seq 1 30`; do echo student$i; done
share|improve this answer

The following script gives you an output like

0.01, 0.02, 0.03 ... until 0.2

/

#!/bin/bash
for ((i=0; i < 20; i += 1))
do
        echo "scale=2; ${i}/100" | bc
done
share|improve this answer

Bash can only do math with integers, not floats, so you either have to use a different language, like awk or bc, or shift the problem into integer domain.

With awk:

awk 'BEGIN{ for (i=0.0; i < 0.2; i+=0.01) printf("%.2f\n", i); }'

With bash, using integers

for ((i=0; i < 20; i++)); do
    printf "%d.%02d\n" $((i/100)) $((i%100))
done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.