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.
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 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 is used to find the difference between two numbers.
#!/bin/basha=20b=8result=$((a - b))echo "Subtraction result: $result"
Output:
Subtraction result: 12
Use the * operator for multiplication.
#!/bin/basha=6b=4result=$((a * b))echo "Multiplication result: $result"
Output:
Multiplication result: 24
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.
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.
#!/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.
Arithmetic operations are often used to increase or decrease values.
#!/bin/bashcount=1((count++))echo "After increment: $count"((count--))echo "After decrement: $count"
#!/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.
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.
The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…
The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…
The top command in Linux provides a real-time view of running processes and system resource usage. From…
The usermod command in Linux modifies existing user account attributes. You can use it to manage group…
The sort command in Linux reads lines from files or standard input and writes them to standard…
The wall command in Linux sends a message to the terminals of all currently logged-in users. The…