BASH math


Here's the syntax for evaluating some simple arithmetic expressions in the Bourne Again Shell (bash). The pound sign indicates a comment. The commands follow the comment.
Print the sum of 2 and 3 (note spaces).

# Put spaces on either side of the plus sign "+".

expr 2 + 3


Print the result of 2 minus 3 (note spaces).

# Put spaces on either side of the minus sign "-".

expr 2 - 3


Print the sum of 2 and 3 (note spaces).

# Put spaces on either side of the plus sign "+".

eval expr 2 + 3


Divide 10 by 2 using integer division.

expr 10 / 2


Divide 10 by 3 using integer division.

# 10 / 3 gives 3, which is the whole part of the division,
# not the remainder.

expr 10 / 3


Divide 10 by 3, using modular arithmetic.

# 10 % 3 gives 1, which is the remainder of the division.
# 10 % 3 is referred to as "10 mod 3". 

expr 10 % 3


Print 2 plus 4 divided by 2.

expr  2 + 4 / 2


Divide the sum of 2 and 4 (6) by 2.

# The backslash "\" is required as a prefix to the parenthesis "(".

expr \( 2 + 4 \) / 2


Set the variable "p" equal to the value "2".

# To access the value of the variable "p",
# prefix p with a dollar sign "$p".
# Use no spaces on either side of the equals sign "=".

p=2
echo $p


Set some variables and do arithmetic with them.

# Set the variable "p" equal to the value "2".
# Set the variable "q" equal to the value "3".
# Evaluate p+q and print the result.
# Dollar signs "$" must prefix a variable to access the
# the contents of the variable.
# Use no spaces on either side of the equals sign "=".
# Do put spaces on either side of the plus sign "+".

p=2
q=3
expr $p + $q 


Set a variable equal to the result of an expression.

# Set the variable "p" equal to the value "s".
# Set the variable "q" equal to the value "3".
# Evaluate p+q and put the result in the variable "r". 
# Print the result.
# Single FORWARD quotes around an expression are used to 
# execute the expression as a command.
# r=`expr $p + $q` says to set the variable "r" equal to the
# result of the command: expr $p + $q

p=2
q=3
r=`expr $p + $q`
echo $r


Some common synatx mistakes.

# Explain why the following commands do not print
# the sum of p and q.

p=2
q=3
r=p+q
echo r

r=$p + $q
echo $r


Concatenate q and p, If q=x and p=y then s=xy.

# Use no spaces on either side of the equals sign "=".

p=2
q=3
s=$q$p
echo "pq is " $p$q
echo "qp is " $q$p
echo "s is the same as qp," $s


Set s equal to the result of "factor 24", print the result.

# Use no spaces on either side of the equals sign "=".
# The single quotes are FORWARD QUOTES, not back quotes.

s=`factor 24`
echo $s

## The bash shell will not perform arithmetic
## with decimals or fractions directly.
## Use "bc" to do floating point arithmetic
## in the bash shell.


Multiply 2 times 3.

# no spaces to the left or right of the "*"

echo 2*3 | bc


Divide 10.5 by 2.

# the "-l" switch tells bc to define the standard math library
# for performing floating point arithmetic.

echo 10.5 / 2 | bc -l
echo 10.5 /2 | bc -l
echo 10.5/2 | bc -l


Convert 32 to hex (base 16).

echo "obase=16; 32" | bc


Convert 23 to binary (base 2).

echo "obase=2; 23" | bc


Use bc to set the bash variable "pi" correct to 4 decimal places

# a(x) is the arctan function. 
# a(1) is the arctangent of 1, pi/4, or 45 degrees.
# Single FORWARD quotes around an expression are used to 
# execute the expression as a command.

pi=`echo "scale=5; 4*a(1)" | bc -l`
echo $pi


# The following syntax also works.

pi=$(echo "scale=5; 4*a(1)" | bc -l)
echo $pi


Web Resources
The bash shell comes with any Linux distribution. You can also get it at www.gnu.org. bash at www.gnu.org/ http://www.gnu.org/software/bash/bash.html Main Outline