Decimal numbers in bash script variables?
Is it possible to store fractional numbers in variables in bash scripts? I can only seem to save integers.
If it's not possible, does anyone know how I could do fractional calculations using only integers? |
This is a shot in the dark for me, but did you try to put the numbers in quotes? It works for white spaces, not sure about decimal numbers.
|
Yeah I tried that.
I actually found my answer. Google :D Bash itself cannot support floating point numbers, but there is a program called bc that can do decimal arithmetic. here's the basic syntax answer=$(echo "scale=9; $x *1.423" | bc) The scale value is the number of decimal spaces you want. I'm still not sure if you can initialize a variable to a decimal number, but I got my stuff working. |
You store anything as a "string", but when you want to calculate, you'll have to use bc ...
|
All times are GMT -5. The time now is 01:29 AM. |