Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

let "k += 0.025"


let "$k += 0.025"


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 BinaryMisfit 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
up vote 3 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)


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.