Skip to content

Latest commit

 

History

History
54 lines (39 loc) · 1.27 KB

2.md

File metadata and controls

54 lines (39 loc) · 1.27 KB

Chapter 3: The Operator Odyssey 🤖

2 - Arithmatic operations using operators

Arithmetic Operations Using Operators in Shell Script

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.

Basic Arithmetic Operators

  • 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.

Example Usage

#!/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

⬅️Previous-----------Next ➡️

Table of contents 🚀