The inbuilt BASH does not support floating point arithmetic calculations but we can easily declare a function to do so via AWK.
#!/bin/bash
# calc.sh
function calc() {
awk "BEGIN { print $*; }"
}
calc $*
You can also export this function in your ~/.bashrc file. Example usage over the command line:
$ ./calc.sh 355/113
3.14159
$ ./calc.sh "3/2**2"
0.75
$ ./calc.sh "(3/2)**2"
2.25
We pass the parameter to BASH function which invokes the AWK to evaluate the arithmetic result from the expression using the printf function.
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: Teaching Kids Programming - Introducing the Chain Function in Python
Next Post: Teaching Kids Programming - Build Array from Permutation