Arithmetic operations in shell scripting allow you to perform mathematical calculations using various operators. These operations can be useful for tasks such as data manipulation, numerical analysis, and automation.
- Addition (+): Adds two values together.
- Subtraction (-): Subtracts one value from another.
- Multiplication (*): Multiplies two values.
- Division (/): Divides one value by another.
- Modulo (%): Returns the remainder of a division operation.
#!/bin/bash
# Define variables
NUM1=10
NUM2=5
# Perform arithmetic operations
SUM=$((NUM1 + NUM2))
DIFF=$((NUM1 - NUM2))
PRODUCT=$((NUM1 * NUM2))
QUOTIENT=$((NUM1 / NUM2))
REMAINDER=$((NUM1 % NUM2))
# Print results
echo "Sum: $SUM"
echo "Difference: $DIFF"
echo "Product: $PRODUCT"
echo "Quotient: $QUOTIENT"
echo "Remainder: $REMAINDER"
After excuting the above script you will get below output :
Sum: 15
Difference: 5
Product: 50
Quotient: 2
Remainder: 0