• Contact
  • About Me
  • Privacy Policy
  • Disclaimer
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • 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
    • Bash Scripts
  • 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

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

Lesson 9: Bash Maths Operations (Bash Arithmetic) Examples

0
SHARES
8
VIEWS
Share on FacebookShare on Twitter
Table of Contents
  • Lesson 9: Bash Maths Operations (Bash Arithmetic) Examples
    • Introduction
    • Floating Point Arithmetic Operations

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

ADVERTISEMENT
Previous Post

Lesson 8: Working with Bash Strings

Next Post

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

neo

neo

Related Posts

Lesson 11: Practical Linux Bash Examples and Projects
Bash Scripting Tutorials

Lesson 11: Practical Linux Bash Examples and Projects

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

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

Lesson 8: Working with Bash Strings
Bash Scripting Tutorials

Lesson 8: Working with Bash Strings

Lesson 7: Bash Functions with Examples
Bash Scripting Tutorials

Lesson 7: Bash Functions with Examples

Lesson 6: Using Bash Command Substitution
Bash Scripting Tutorials

Lesson 6: Using Bash Command Substitution

Lesson 5: Linux Bash Read User Input with Example
Bash Scripting Tutorials

Lesson 5: Linux Bash Read User Input with Example

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

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

Recommended

Solutions: Add New Hard Drive (HDD or SSD) to Ubuntu

Add and Mount New HDD or SSD on Ubuntu – Easy Tutorial

Linux Directory Navigation and Creation with cd, pwd, and mkdir

Automatic Windows Service Monitoring and Restart Script

Automatic Windows Service Monitoring and Restart Script

10+ Useful Linux find Command Examples You Should Know

10+ Useful Linux find Command Examples You Should Know

Host a Website on Raspberry Pi – Real Benchmark Tests

Host a Website on Raspberry Pi – Real Benchmark Tests

Categories

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

Recent News

Automatic Windows Service Monitoring and Restart Script

Automatic Windows Service Monitoring and Restart Script

10+ Useful Linux find Command Examples You Should Know

10+ Useful Linux find Command Examples You Should Know

  • 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
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos

© 2025 defencedev.com - All rights reserved.