Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I wanted to increment a variable, k inside a loop. Each increment is by 0.025. I tried using:

let "k += 0.025"

and

let "$k += 0.025"

and

k += 0.025

and many other variations. Does anyone know how to accomplish this?

Thanks! Amit

share|improve this question

closed as off topic by Diago Mar 3 '11 at 7:06

Questions on Super User are expected to relate to computer software or computer hardware within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Please don't cross-post. –  Dennis Williamson Mar 2 '11 at 23:44
    
Sorry, I wasn't sure how many users are on superuser, and I've had good experience with response-time on SO –  Amit Mar 2 '11 at 23:45

1 Answer 1

up vote 2 down vote accepted

Bash doesn't support floating point. Ksh93 and zsh do.

In Bash, you can use bc or awk:

k=$(echo "$k + 0.025" | bc -l)

or

k=$(awk -v "k=$k" 'BEGIN { print k + 0.025 }')

In some cases it might be more convenient to write whole scripts or sections in AWK rather than try to pass values in and out and do awkward comparisons.

share|improve this answer
    
I tried using the bc command you gave me and I get line 7: syntax error near unexpected token k=$(echo "$k + 0.025" | bc -l)' ` –  Amit Mar 2 '11 at 23:47
    
@Amit: It works fine for me. Note that k would need to be initialized. If it's unset, bc will issue an error. The error you received is from the shell, however. Are you using Bash? What version? –  Dennis Williamson Mar 2 '11 at 23:57

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