How To

How To Use Arithmetic Operations In Bash Scripts

Introduction

Arithmetic operations are an important part of Bash scripting. They allow you to perform calculations such as addition, subtraction, multiplication, division, and modulus directly inside a script. If you are learning Bash scripting, arithmetic is useful for counters, loops, system checks, automation tasks, backup scripts, and cybersecurity tools.

Bash is mainly a shell language, but it supports integer-based arithmetic. This means you can calculate numbers without using external tools in most basic cases.

Basic Arithmetic Syntax In Bash

The most common way to perform arithmetic in Bash is using double parentheses:

$(( expression ))

Example:

echo $((5 + 3))

Output:

8

You can use this syntax inside scripts, variables, loops, and conditions.

Addition In Bash

Addition is used to add two or more numbers.

Create a script:

nano arithmetic.sh

Add the following code:

#!/bin/basha=10b=5sum=$((a + b))echo "Addition result: $sum"

Run the script:

chmod +x arithmetic.sh./arithmetic.sh

Output:

Addition result: 15

Subtraction In Bash

Subtraction is used to find the difference between two numbers.

#!/bin/basha=20b=8result=$((a - b))echo "Subtraction result: $result"

Output:

Subtraction result: 12

Multiplication In Bash

Use the * operator for multiplication.

#!/bin/basha=6b=4result=$((a * b))echo "Multiplication result: $result"

Output:

Multiplication result: 24

Division In Bash

Use the / operator for division.

#!/bin/basha=20b=5result=$((a / b))echo "Division result: $result"

Output:

Division result: 4

Remember, Bash arithmetic works with integers by default. If you divide 5 / 2, the result will be 2, not 2.5.

Modulus In Bash

The modulus operator % returns the remainder after division.

#!/bin/basha=10b=3result=$((a % b))echo "Remainder: $result"

Output:

Remainder: 1

This is useful when checking even or odd numbers.

Check Even Or Odd Number

#!/bin/bashread -p "Enter a number: " numberif [[ $((number % 2)) -eq 0 ]]; then    echo "$number is even"else    echo "$number is odd"fi

This script asks the user for a number and checks whether it is even or odd.

Increment And Decrement Values

Arithmetic operations are often used to increase or decrease values.

#!/bin/bashcount=1((count++))echo "After increment: $count"((count--))echo "After decrement: $count"

Cybersecurity Example: Count Failed SSH Login Attempts

#!/bin/bashfailed_count=$(grep "Failed password" /var/log/auth.log | wc -l)echo "Total failed SSH login attempts: $failed_count"if [[ $failed_count -gt 10 ]]; then    echo "Warning: High number of failed login attempts detected"else    echo "Failed login attempts are normal"fi

This script counts failed SSH login attempts and displays a warning if the count is high.

Conclusion

Arithmetic operations in Bash are simple but very useful. You can perform addition, subtraction, multiplication, division, modulus, increment, and decrement operations using $(( )).

For beginners, learning Bash arithmetic helps in writing better scripts for automation, counters, loops, system checks, reports, and cybersecurity monitoring. Once you understand arithmetic operations, your Bash scripts can make smarter calculations and decisions.

Cyber Defence

Recent Posts

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

1 day ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

1 day ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

1 day ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

1 day ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

1 day ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

2 days ago