Difference between revisions of "bash arithmetic"

From thelinuxwiki
Jump to: navigation, search
(Created page with "examples a=`expr $a + 1` a=`expr 5 + 3` category:bash")
 
Line 1: Line 1:
examples
+
 
 +
== using let ==
 +
 
 +
let "TOTAL = ( $A + 5 ) / $b"
 +
 
 +
 
 +
== floating point using bc ==
 +
 
 +
TOTAL=`echo "( $A + 5 ) / $b" | bc -l
 +
 
 +
'''using scale to control number of digits'''
 +
 
 +
$ echo "1 / 100" | bc -l
 +
.01000000000000000000
 +
 +
$ echo "scale =2; 1 / 100" | bc -l                                                                        ~
 +
.01
 +
 
 +
 
 +
using expr
 
  a=`expr $a + 1`
 
  a=`expr $a + 1`
  

Revision as of 17:04, 23 August 2013

using let

let "TOTAL = ( $A + 5 ) / $b"


floating point using bc

TOTAL=`echo "( $A + 5 ) / $b" | bc -l

using scale to control number of digits

$ echo "1 / 100" | bc -l 
.01000000000000000000

$ echo "scale =2; 1 / 100" | bc -l                                                                        ~
.01


using expr

a=`expr $a + 1`
a=`expr 5 + 3`