There are two ways we can have the shell evaluate an expression. Either using
$((expression)) syntax or the
The former does not require proper spacing, while the latter does.
$ echo $((2+3)) 5 $ expr 2 + 3 5
Arithmetic expasion only supports integer values and no decimals.
$ expr 2.3 + 2 expr: not a decimal number: '2.3'
Here are a list of arithmetic operators.
It's important to avoid using the
= symbol when comparing numerals. The
= looks for string equality, not numeric equality. Thus, when working with arithmetic equations, be sure to use
$ [ 1 = 1 ]; echo $? 0 $ [ 092 = 92 ]; echo $? 1 $ [ 092 -eq 92 ]; echo $? 0
Here is a list of more comparison tests we can use to compare two numbers. Be sure to not use canonical notations such as
$ [ 92 > 32 ]; echo $? 0 # Looks good...(but wrong) $ [ 92 < 32 ]; echo $? 0 # Wrong $ [ 92 -gt 32 ]; echo $? 0 # There we go!
The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.$ Check price
Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.$ Check price