Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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 use bash calculator for floating point calculations and I have to use input redirection and backtick (`) symbol in scripts.

As normal bracket $[math operation] and expr doesn't support float calculation. Is there any other way to do float calculation directly instead of using bc in script. I don't like unnecessary input redirection and backtick (`) symbol for scripts.

#!/bin/bash
x=5
y=6
z=3.3
result=`bc <<end
scale=3
temp_divide=($x / $y)
temp_divide * $z
end`
echo "final result is $result"
share|improve this question
up vote 1 down vote accepted

Bash doesn't support floating point arithmetics, so you are stuck with using bc for that. You could use self-written helper functions similar to float_eval, which makes using bc as an inline calculator a little bit easier.

If you don't have to use bash, you could also consider using zsh, which supports floating point operations. Example:

evnu@centraldogma ~ 
% ((val = 1.0))
evnu@centraldogma ~ 
% ((val = 2.2))
evnu@centraldogma ~ 
% echo $val
2.2000000000
evnu@centraldogma ~ 
% ((val += 2.2))
evnu@centraldogma ~ 
% echo $val
4.4000000000
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .