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

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

51 minutes ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

5 hours ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

6 hours ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

7 hours ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

8 hours ago

How To Work With Files And Directories Using Bash Scripts

Introduction Working with files and directories is one of the most important skills in Bash…

9 hours ago