SoFunction
Updated on 2025-04-22

Practical tips and precautions for four operations in shell scripts

Preface

In Shell scripting, handling mathematical operations is a common task. Whether it is simple addition or complex expression calculations, it is crucial to master the four operator symbols and how to use them in Shell scripts. This article will introduce in detail how to perform four operations (addition, subtraction, multiplication, and division) in shell scripts, and explore some practical tips and precautions.

1. Basic four operating symbols

Shell scripts support basic arithmetic operations, including addition, subtraction, multiplication and division. These operations can be achieved in a number of ways, the most common method is to useexprCommand or$((...))grammar.

(I) Addition

use+to perform addition operations.

Using expr:

result=$(expr 5 + 3)
echo "The result is $result"

Use $((...)):

result=$((5 + 3))
echo "The result is $result"

(Bi) Subtraction

use-to perform subtraction operation.

Using expr:

result=$(expr 10 - 4)
echo "The result is $result"

Use $((...)):

result=$((10 - 4))
echo "The result is $result"

(Three) Multiplication

use*to perform multiplication operation. Note, in useexprWhen the asterisk needs to be escaped, the$((...))If it is medium, it is not needed.

Using expr:

result=$(expr 6 \* 7)
echo "The result is $result"

Use $((...)):

result=$((6 * 7))
echo "The result is $result"

(IV) Division

use/to perform division operation. It should be noted that integer division will discard the fractional part.

Using expr:

result=$(expr 20 / 4)
echo "The result is $result"

Use $((...)):

result=$((20 / 4))
echo "The result is $result"

2. Floating point number operation

By default, Shell only supports integer operations. If you need to perform floating point calculations, you can use the helpbcCommand (an arbitrary precision calculator language).

(I) Basic usage

usebcWhen the command performs floating point operation, the expression can be passed to thebc

Example:

result=$(echo "scale=2; 20.5 / 4" | bc)
echo "The result is $result"

herescale=2It means that the result retains two decimal places.

(II) Use in combination with variables

You can also insert variables intobcComputation is performed in the expression.

Example:

num1=20.5
num2=4
result=$(echo "scale=2; $num1 / $num2" | bc)
echo "The result is $result"

3. Self-increase and self-decrease

In shell scripts, you can also use autoincrement (++) and self-decreasing (--) operator to change the value of a numeric variable.

(I) Self-increase

counter=5
((counter++))
echo "After increment: $counter" # Output: 6
counter=5
((++counter))
echo "After pre-increment: $counter" # Output: 6

(II) Self-decreasing

counter=5
((counter--))
echo "After decrement: $counter" # Output: 4
counter=5
((--counter))
echo "After pre-decrement: $counter" # Output: 4

4. Compound assignment operator

In addition to basic four-character operations, Shell also supports composite assignment operators, such as+=-=*=/=wait.

Example:

a=5
((a += 3)) # is equivalent to a=a+3echo "After adding 3: $a" # Output: 8
b=10
((b -= 4)) # is equivalent to b=b-4echo "After subtracting 4: $b" # Output: 6
c=6
((c *= 7)) # is equivalent to c=c*7echo "After multiplying by 7: $c" # Output: 42
d=20
((d /= 4)) # is equivalent to d=d/4echo "After dividing by 4: $d" # Output: 5

Summarize

This is the article about the practical tips and precautions of four-character operator symbols in Shell scripts. For more related contents of four-character operator symbols in Shell scripts, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!