The inbuilt BASH does not support floating point arithmetic calculations but we can easily declare a function to do so via AWK.
1 2 3 4 5 6 7 8 | #!/bin/bash # calc.sh function calc() { awk "BEGIN { print $*; }" } calc $* |
#!/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:
1 2 3 4 5 6 | $ ./calc.sh 355/113 3.14159 $ ./calc.sh "3/2**2" 0.75 $ ./calc.sh "(3/2)**2" 2.25 |
$ ./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) —
GD Star Rating
loading...
154 wordsloading...
Last Post: Teaching Kids Programming - Introducing the Chain Function in Python
Next Post: Teaching Kids Programming - Build Array from Permutation