## Testing Numeric Values

Bash scripting number tests

### Testing Numbers

As we have seen so far we can check files and compare strings. We also can easily compare numerical values. The table below indicates some of the tests that may be performed in conjunction with the "if" statement:

### Numeric Tests within Parenthesis:

Operator |
Description | Example |
---|---|---|

> | Greater than | (( x > y )) |

>= | Greater than or equal to | (( x >= y )) |

< | Less than | (( x < y )) |

<= | Less than or equal to | (( x <= y)) |

!= | Not equal to | (( x != y)) |

! | Logical Not | (( !(x < y) )) |

== | Equal to. | (( x == y)) |

### Assignment and Equality - "=" and "=="

If you are carrying out a test where you are checking for equality (two identical values) then you must use the double equals operator "**==**". A single equals operator"**=**" is used for assignment.

If we wanted to assign a value to a variable we would use the syntax: **x=10**

If we wanted to check for equality we would use a structure similar to: **if (( x==10); then**

**Example Numerical Tests within an "if" statement:**

**
#!/bin/bash
x=10
y=20
printf "\nThe variable \$x has been assigned a value of $x \nThe variable \$y has been assigned a value of $y\n"
printf "\nTest x > y : x=$x y=$y\n"
if (( x > y )); then
printf "x is greater than y\n"
else
printf "x is less than y\n"
fi
printf "\nTest x >= y : x=$x y=$y\n"
if (( x >= y )); then
printf "x is greater than or equal to y\n"
else
printf "x is less than y\n"
fi
printf "\nTest x < y : x=$x y=$y\n"
if (( x < y ));then
printf "x is less than y\n"
else
printf "x is greater than y\n"
fi
printf "\nTest x <= y : x=$x y=$y\n"
if (( x <= y ));then
printf "x is less than or equal to y\n"
else
printf "x is greater than y\n"
fi
printf "\nTest x != y : x=$x y=$y\n"
if (( x != y ));then
printf "x is not equal to y\n"
else
printf "x is equal to y\n"
fi
printf "\nTest x == y : x=$x y=$y\n"
if (( x == y ));then
printf "x is equal to y\n"
else
printf "x is not equal to y\n"
fi
**

Now if we run the above script, we will receive the following output:

**
john@john-desktop:~/scripts$ ./test09.sh
The variable $x has been assigned a value of 10
The variable $y has been assigned a value of 20
Test x > y : x=10 y=20
x is less than y
Test x >= y : x=10 y=20
x is less than y
Test x < y : x=10 y=20
x is less than y
Test x <= y : x=10 y=20
x is less than or equal to y
Test x != y : x=10 y=20
x is not equal to y
Test x == y : x=10 y=20
x is not equal to y
**

### Other Integer Operators

As we saw earlier we can use double bracket parenthesis style tests. However, you may also use the following syntax:

Operator |
Description | Example |
---|---|---|

eq | Equal to | [ $x -eq 20 ] |

ne | Not equal to | [ $x -ne 20 ] |

gt | Greater than | [ $x -gt 20 ] |

ge | Greater than or equal to | [ $x -ge 20 ] |

lt | Less than | [ $x -lt 20 ] |

le | Less than or equal to | [ $x -le 20 ] |

**Example Script:**

**
#!/bin/bash
x=10
printf "\nThe variable \$x has been assigned a value of $x\n"
printf "\nTest x -eq 20 : x=$x\n"
if [ $x -eq 20 ];then
printf "x is equal to 20\n"
else
printf "x is not equal to 20\n"
fi
printf "\nTest x -ne 20 : x=$x\n"
if [ $x -ne 20 ];then
printf "x is not equal to 20\n"
else
printf "x is equal to 20\n"
fi
printf "\nTest x -gt 20 : x=$x\n"
if [ $x -gt 20 ];then
printf "x is Greater than 20\n"
else
printf "x is not Greater than 20\n"
fi
printf "\nTest x -ge 20 : x=$x\n"
if [ $x -ge 20 ];then
printf "x is Greater than or equal to 20\n"
else
printf "x is not Greater than or equal to 20\n"
fi
printf "\nTest x -lt 20 : x=$x\n"
if [ $x -lt 20 ];then
printf "x is less than 20\n"
else
printf "x is not less than 20\n"
fi
printf "\nTest x -le 20 : x=$x\n"
if [ $x -le 20 ];then
printf "x is less than or equal to 20\n"
else
printf "x is not less than or equal to 20\n"
fi
**

**Output from Example Script:**

**
john@john-desktop:~/scripts$ ./test10.sh
The variable $x has been assigned a value of 10
Test x -eq 20 : x=10
x is not equal to 20
Test x -ne 20 : x=10
x is not equal to 20
Test x -gt 20 : x=10
x is not Greater than 20
Test x -ge 20 : x=10
x is not Greater than or equal to 20
Test x -lt 20 : x=10
x is less than 20
Test x -le 20 : x=10
x is less than or equal to 20
**