I am trying to divide two var in bash, this is what I've got:
var1=3;
var2=4;
echo ($var1/$var2)
I always get a syntax error. Does anyone knows what's wrong?
I am trying to divide two var in bash, this is what I've got:
var1=3;
var2=4;
echo ($var1/$var2)
I always get a syntax error. Does anyone knows what's wrong?
If you want to do it without bc, you could use awk:
shell parsing is useful only for integer division:
output: 2
instead your example:
ouput: 0
it's better to use bc:
output: .75
scale is the precision required
There are two possible answers here.
To perform integer division, you can use the shell:
The
$(( ... ))
syntax is known as an arithmetic expansion.For floating point division, you need to use another tool, such as
bc
:The
scale=2
statement sets the precision of the output to 2 decimal places.