I want to make a for loop in bash with 0.02 as increments I tried
for ((i=4.00;i<5.42;i+=0.02))
do
commands
done
but it didn't work.
I want to make a for loop in bash with 0.02 as increments I tried
for ((i=4.00;i<5.42;i+=0.02))
do
commands
done
but it didn't work.
Avoid loops in shells.
If you want to do arithmetic, use awk or bc:
awk '
BEGIN{
for (i = 4.00; i < 5.42; i+ = 0.02)
print i
}'
Or
bc << EOF
for (i = 4.00; i < 5.42; i += 0.02) i
EOF
Note that awk (contrary to bc) works with your processors double floating point number representation (likely IEEE 754 type). As a result, since those numbers are binary approximations of those decimal numbers, you may have some surprises:
$ gawk 'BEGIN{for (i=0; i<=0.3; i+=0.1) print i}'
0
0.1
0.2
If you add a OFMT="%.17g" you can see the reason for the missing 0.3:
$ gawk 'BEGIN{OFMT="%.17g"; for (i=0; i<=0.5; i+=0.1) print i}'
0
0.10000000000000001
0.20000000000000001
0.30000000000000004
0.40000000000000002
0.5
bc does arbitrary precision so doesn't have this kind of problem.
Note that by default (unless you modify the output format with OFMT or use printf with explicit format specifications), awk uses %.6g for displaying floating point numbers, so would switch to 1e6 and above for floating point numbers above 1,000,000 and truncate the fractional part for high numbers (100000.02 would be displayed as 100000).
If you do really need to use a shell loop, because for instance you want to run specific commands for each iteration of that loop, either use a shell with floating point arithmetic support like zsh, yash or ksh93 or generate the list of values with one command as above (or seq if available) and loop over its output.
Like:
unset -v IFS # configure split+glob for default word splitting
for i in $(seq 4 0.02 5.42); do
something with "$i"
done
Or:
seq 4 0.02 5.42 | while IFS= read i; do
something with "$i"
done
unless you push the limits of your processor floating point numbers, seq handles errors incurred by floating point approximations more gracefully than the awk version above would.
If you don't have seq (a GNU command), you can make a more reliable one as a function like:
seq() { # args: first increment last
bc << EOF
for (i = $1; i <= $3; i += $2) i
EOF
}
That would work better for things like seq 100000000001 0.000000001 100000000001.000000005. Note however that having numbers with arbitrarily high precision won't help much if we're going to pass them to commands which don't support them.
Reading the bash man page gives the following information:
for (( expr1 ; expr2 ; expr3 )) ; do list ; doneFirst, the arithmetic expression
expr1is evaluated according to the rules described below under ARITHMETIC EVALUATION. [...]
and then we get this section
ARITHMETIC EVALUATION
The shell allows arithmetic expressions to be evaluated, under certain circumstances (see the
letanddeclarebuiltin commands and Arithmetic Expansion). Evaluation is done in fixed-width integers with no check for overflow [...]
So it can be clearly seen that you cannot use a for loop with non-integer values.
One solution may be simply to multiply all your loop components by 100, allowing for this where you later use them, like this:
for ((k=400;k<542;k+=2))
do
i=$(bc <<<"scale=2; $k / 100" ) # when k=402 you get i=4.02, etc.
...
done
If you starting and ending values are the same number of significant figures (three in this example) you can avoid the call out to bc for each iteration of the loop and instead use string processing to generate the decimal value,
i="${k%??}.${k#?}" # POSIX; when k=402 you get i=4.02, etc.
i="${k:0:1}.${k:1}" # bash; when k=402 you get i=4.02, etc.
Use "seq" - print a sequence of numbers
seq FIRST INCREMENT LAST
for i in $(seq 4.00 0.02 5.42)
do
echo $i
done
As others have suggested, you can use bc:
i="4.00"
while [[ "$(bc <<< "$i < 5.42")" == "1" ]]; do
# do something with i
i="$(bc <<< "$i + 0.02")"
done