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

groupdel Command in Linux: Remove a Group and Audit Files

The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…

12 hours ago

wc Command in Linux: Count Lines, Words, Characters, and Bytes

The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…

12 hours ago

top Command in Linux: Monitor Processes and Resource Usage

The top command in Linux provides a real-time view of running processes and system resource usage. From…

12 hours ago

usermod Command in Linux: Modify User Accounts and Groups

The usermod command in Linux modifies existing user account attributes. You can use it to manage group…

12 hours ago

sort Command in Linux: Sort Text, Numbers, Columns, and More

The sort command in Linux reads lines from files or standard input and writes them to standard…

2 days ago

wall Command in Linux: Broadcast Messages to Logged-In Users

The wall command in Linux sends a message to the terminals of all currently logged-in users. The…

2 days ago