The (( )) construct expands and evaluates an arithmetic expression. If the expression evaluates as zero, it
returns an exit status of 1, or "false". A non-zero expression returns an exit status of 0, or "true". This is in
marked contrast to using the test and [ ] constructs previously discussed.
#!/bin/bash
# arith-tests.sh# Arithmetic tests.
# The (( ... )) construct evaluates and tests numerical expressions.
# Exit status opposite from [ ... ] construct!
echo "Exit status of \"(( 0 ))\" is $?."
echo "Exit status of \"(( 1 ))\" is $?."
echo "Exit status of \"(( 5 > 4 ))\" is $?." # 0
echo "Exit status of \"(( 5 > 9 ))\" is $?." # 1
echo "Exit status of \"(( 5 == 5 ))\" is $?." # 0
(( 5 - 5 )) # 0
echo "Exit status of \"(( 5 - 5 ))\" is $?." # 1
echo "Exit status of \"(( 5 / 4 ))\" is $?." # 0
echo "Exit status of \"(( 1 / 2 ))\" is $?." # Rounded off to 0.
# 1
(( 1 / 0 )) 2>/dev/null # Illegal division by 0.
#
^^^^^^^^^^^
echo "Exit status of \"(( 1 / 0 ))\" is $?."
# 1
# What effect does the "2>/dev/null" have?
# What would happen if it were removed?
# Try removing it, then rerunning the script.
# ======================================= #
# (( ... )) also useful in an if-then test.
var1=5
var2=4
if (( var1 > var2 ))
then #Note: Not $var1, $var2. Why?
echo "$var1 is greater than $var2"
fi # 5 is greater than 4
exit 0