• Contact
  • About Me
  • Privacy Policy
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
DefenceDev
No Result
View All Result
ADVERTISEMENT
Home Linux Tutorials Bash Scripting Tutorials

Lesson 9: Bash Maths Operations (Bash Arithmetic) Examples

by neo
May 27, 2025
in Bash Scripting Tutorials
0
Lesson 9: Bash Maths Operations (Bash Arithmetic) Examples

Lesson 9: Bash Maths Operations (Bash Arithmetic) Examples

0
SHARES
4
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

Lesson 9: Bash Maths Operations (Bash Arithmetic) Examples

Welcome to Lesson 9: Bash Maths Operations (Bash Arithmetic). In this lesson, we’ll dive into Bash arithmetic and explore how to perform basic mathematical operations in your scripts. Whether you need to calculate values, manipulate numbers, or automate tasks involving numerical data, mastering Bash maths operations is an essential skill for any Bash scripter. You’ll learn how to perform addition, subtraction, multiplication, division, and modulus operations, as well as work with variables for more dynamic calculations. With practical examples, you’ll gain hands-on experience that will empower you to automate complex tasks involving numbers with ease. Let’s get started with Bash arithmetic!

Introduction

Bash handles maths operations differently from many other programming languages.

In this article, you will see some common numerical operations.

More information about floating-point arithmetic you can find at the link.

Arithmetic Operations

In Bash, you can perform arithmetic operations using various methods, including the (( )) syntax, which is specifically designed for arithmetic. Here are some of the basic arithmetic operations in Bash:

num1=5
num2=3
sum=$((num1 + num2))
echo $sum  # Output: 8

Increment and Decrement

Increment and decrement operations are frequently used to increase or decrease a variable’s value. These operations are especially useful in loops, where you need to track the number of iterations or adjust a variable’s value dynamically as the script executes. By leveraging these operations, you can efficiently control loops or perform repetitive tasks in Bash scripts.

num=5
((num++))
echo $num  # Output: 6
((num--))
echo $num  # Output: 5

Comparison Operations

Comparison operations in Bash allow you to compare two values and assess their relationship—whether one value is greater than, less than, or equal to another. These operations play a crucial role in controlling the flow of a script, particularly when used in conditional statements like if and while. By performing comparisons, you can make decisions based on the outcomes, enabling dynamic and flexible script execution.

num1=5
num2=3
if [ $num1 -gt $num2 ]; then
    echo "$num1 is greater than $num2"
else
    echo "$num1 is not greater than $num2"
fi

Floating Point Arithmetic Operations

num1=5.5
num2=3.2
result=$(echo "$num1 + $num2" | bc)
echo $result  # Output: 8.7

Practical Examples

In this section, we’ll dive into practical examples that showcase the power of Bash for performing arithmetic operations and processing text. By applying Bash to real-world scenarios, you can significantly enhance your scripts’ capabilities. Let’s explore these examples:

ADVERTISEMENT
ADVERTISEMENT
Counting Words in a String
str="Hello World from Bash"
word_count=$(echo $str | wc -w)
echo "Word count: $word_count"  # Output: Word count: 4
Calculating the Sum of Numbers in a File
sum=0
while read num; do
    sum=$((sum + num))
done < numbers.txt
echo "Sum: $sum"

defencedev.com

ADVERTISEMENT

«Previous: Lesson 8: Working with Bash Strings
Next: Lesson 10: Bash File Operation (Read, Write, Copy)»


About The Author

neo

See author's posts

Tags: bash-scripting
ADVERTISEMENT
Previous Post

Lesson 8: Working with Bash Strings

Next Post

Lesson 10: Bash File Operation (Read, Write, Copy)

neo

Next Post
Lesson 10: Bash File Operation (Read, Write, Copy)

Lesson 10: Bash File Operation (Read, Write, Copy)

Follow Us

  • Trending
  • Comments
  • Latest
MikroTik: Export Configuration in Text File

MikroTik: Export Configuration in Text File

Fortinet FortiGate: Static Route Configuration via GUI and CLI

Fortinet FortiGate: Static Route Configuration via GUI and CLI

Fortinet FortiGate Upgrade Path Tool

Fortinet FortiGate Upgrade Path Tool

Immich: Installation on Ubuntu 22.04

Immich: Installation on Ubuntu 22.04

NextCloud: Port Forwarding - Essential Ports for Smooth Functionality

NextCloud: Port Forwarding – Essential Ports for Smooth Functionality and Remote Access

Organizing and Managing Photos with Immich: Features I Use

Organizing and Managing Photos with Immich: Features I Use

Install Ubuntu 22.04 on Oracle VM Virtual Box

Install Ubuntu 22.04 on Oracle VM Virtual Box

Linux Directories: cd (Change Directory), pwd (Print Working Directory), mkdir (Make Directory)

Linux Directories: cd (Change Directory), pwd (Print Working Directory), mkdir (Make Directory)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

MikroTik: Check Your Wireless Password

MikroTik: Check Your Wireless Password

Install SoftEther VPN Server on Ubuntu 24.04

Install SoftEther VPN Server on Ubuntu 24.04

Recent News

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

MikroTik: Check Your Wireless Password

MikroTik: Check Your Wireless Password

Install SoftEther VPN Server on Ubuntu 24.04

Install SoftEther VPN Server on Ubuntu 24.04

Timeshift on Linux: How to Create and Restore System Snapshots

Timeshift on Linux: How to Create and Restore System Snapshots

ADVERTISEMENT

DefenceDev Tutorials

defencedev Logo

Whether you’re just starting or looking to expand your skills, I hope you find useful information and engaging discussions here. Let me take you through my journey and the goals behind this space!

Follow Us

Browse by Category

  • Blog
  • Cloud
    • Private
  • Linux Tutorials
    • Bash Scripting Tutorials
    • Commands
    • Networking
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Solutions
    • Docker
  • Video Tutorials
    • MikroTik Videos

Recent News

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

  • Site Map
  • Privacy Policy
  • Facebook Page
  • Disclaimer
  • Contact
  • About Me

© 2025 defencedev.com - All rights reserved.

No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos

© 2025 defencedev.com - All rights reserved.