Проверка переполнения

Простые типы / Числа / Integer

#bash numbers are signed 64-bit and there is no range check:
#the result wraps around silently
max=9223372036854775807
wrapped=$((max + 1))
#wrapped is -9223372036854775808

doubled=$((max * 2))
#doubled is -2

#check before the operation: a + b overflows when a > max - b
a=$max
b=1
if (( b > 0 && a > max - b )); then
    overflow=true
else
    overflow=false
fi
#overflow is true

#bc counts with any number of digits, see BigInteger
exact=$(bc <<< "$max + 1")
#exact is 9223372036854775808

echo "wrapped is $wrapped"
echo "doubled is $doubled"
echo "overflow is $overflow"
echo "exact is $exact"