
- Kali Linux Tutorial
- Kali Linux - Home
- Installation & Configuration
- Information Gathering Tools
- Vulnerability Analyses Tools
- Kali Linux - Wireless Attacks
- Website Penetration Testing
- Kali Linux - Exploitation Tools
- Kali Linux - Forensics Tools
- Kali Linux - Social Engineering
- Kali Linux - Stressing Tools
- Kali Linux - Sniffing & Spoofing
- Kali Linux - Password Cracking Tools
- Kali Linux - Maintaining Access
- Kali Linux - Reverse Engineering
- Kali Linux - Reporting Tools
- Kali Linux Useful Resources
- Kali Linux - Quick Guide
- Kali Linux - Useful Resources
- Kali Linux - Discussion
Bash Math Operations (Bash Arithmetic) Explained
Bash math operations or Bash arithmetic refers to mathematical operations that can be performed within a Bash script. Bash is a command-line shell used in many Unix-based systems, including Linux and macOS. Bash arithmetic is a powerful feature of shell, allowing you to perform various math operations like addition, subtraction, multiplication, and division with ease.
In this article, we will explore basics of Bash arithmetic, including its syntax and various operations that can be performed with examples.
Basic Syntax of Bash Arithmetic
To perform Bash arithmetic, you need to use a special syntax that is different from regular arithmetic operators. basic syntax of Bash arithmetic is as follows −
$(( expression ))
The expression is mathematical operation you want to perform, and it can include variables, numbers, and operators. double parentheses are used to indicate that this is a Bash arithmetic operation.
Let's take a look at some examples to understand this syntax better.
Example 1: Addition
To add two numbers using Bash arithmetic, you can use following syntax −
$(( num1 + num2 ))
Here, num1 and num2 are two numbers you want to add. Let's say we want to add 5 and 10. Bash arithmetic expression for this will be −
$(( 5 + 10 ))
The output of this expression will be 15.
Example 2: Subtraction
To subtract two numbers using Bash arithmetic, you can use following syntax −
$(( num1 - num2 ))
Here, num1 and num2 are two numbers you want to subtract. Let's say we want to subtract 10 from 20. Bash arithmetic expression for this will be −
$(( 20 - 10 ))
The output of this expression will be 10.
Example 3: Multiplication
To multiply two numbers using Bash arithmetic, you can use following syntax −
$(( num1 * num2 ))
Here, num1 and num2 are two numbers you want to multiply. Let's say we want to multiply 3 and 4. Bash arithmetic expression for this will be −
$(( 3 * 4 ))
The output of this expression will be 12.
Example 4: Division
To divide two numbers using Bash arithmetic, you can use following syntax −
$(( num1 / num2 ))
Here, num1 and num2 are two numbers you want to divide. Let's say we want to divide 20 by 5. Bash arithmetic expression for this will be −
$(( 20 / 5 ))
The output of this expression will be 4.
Bash Arithmetic Operators
In Bash arithmetic, you can use various operators to perform mathematical operations. Here are some of operators that you can use −
Addition (+)
The addition operator is used to add two numbers. Here's an example −
Example
$(( 5 + 10 ))
Output
15
Subtraction (-)
The subtraction operator is used to subtract two numbers. Here's an example −
Example
$(( 20 - 10 ))
Output
10
Multiplication (*)
The multiplication operator is used to multiply two numbers. Here's an example −
Example
$(( 3 * 4 ))
Output
12
Division (/)
The division operator is used to divide two numbers. Here's an example −
Example
$(( 20 / 5 ))
Output
4
Note − When dividing two numbers, Bash will perform integer division by default. To perform floating-point division, you need to use bc command.
Modulo (%)
The modulo operator is used to get remainder of a division operation. Here's an example −
Example
$(( 10 % 3 ))
Output
1
In this example, we are using modulo operator to find remainder when 10 is divided by 3. output of this expression will be 1, as remainder is 1.
Exponentiation (**)
The exponentiation operator is used to raise a number to a power. Here's an example −
Example
$(( 2 ** 3 ))
Output
8
In this example, we are using exponentiation operator to raise 2 to power of 3, which is 8.
Increment (++)
The increment operator is used to increase value of a variable by 1. Here's an example −
Example
num=5 $(( num++ ))
Output
6
In this example, we are using increment operator to increase value of num variable by 1. output of this expression will be 6.
Decrement (--)
The decrement operator is used to decrease value of a variable by 1. Here's an example −
Example
num=5 $(( num-- ))
Output
4
In this example, we are using decrement operator to decrease value of num variable by 1. output of this expression will be 4.
Variables in Bash Arithmetic
In Bash arithmetic, you can also use variables to perform mathematical operations. Here's an example −
Example
num1=5 num2=10 $(( num1 + num2 ))
Output
15
In this example, we are using two variables num1 and num2 to perform addition operation. output of this expression will be 15.
You can also use variables and operators together to perform more complex mathematical operations. Here's an example −
Example
num1=5 num2=10 $(( (num1 + num2) * 3 ))
Output
45
In this example, we are using variables num1 and num2 to perform addition operation and then multiplying result by 3. output of this expression will be 45.
Using bc Command for Floating-Point Division
As mentioned earlier, when dividing two numbers in Bash arithmetic, result will be an integer by default. If you need to perform floating-point division, you can use bc command. bc command is a command-line calculator that can perform various mathematical operations, including floating-point division. Here's an example −
Example
$ echo "20/3" | bc -l
Output
6.66666666666666666666
In this example, we are using echo command to send expression "20/3" to bc command. -l option is used to load math library, which allows bc command to perform floating-point division. output of this expression will be 6.66666666666666666666, which is result of dividing 20 by 3.
Using Bash Arithmetic in Conditional Statements
Bash arithmetic can also be used in conditional statements, such as if statements and loops. Here's an example −
Example
num=5 if (($num > 0)); then echo "The number is positive." fi
Output
number is positive.
In this example, we are using if statement to check if value of num variable is greater than 0. (($num > 0)) syntax is used to perform comparison operation. If condition is true, message "The number is positive." will be printed.
Similarly, you can use Bash arithmetic in loops to perform various mathematical operations. Here's an example −
Example
for ((i=1; i<=10; i++)); do echo $((i * 2)) done
Output
2 4 6 8 10 12 14 16 18 20
In this example, we are using a for loop to print first 10 even numbers. $((i * 2)) syntax is used to perform multiplication operation.
Conclusion
Bash arithmetic is a powerful feature of Bash shell that allows you to perform various mathematical operations with ease. In this article, we covered basics of Bash arithmetic, including its syntax and various operators. We also looked at how to use variables in Bash arithmetic to perform more complex mathematical operations.
With Bash arithmetic, you can perform various calculations and automate your scripts with ease. By understanding basics of Bash arithmetic, you can take advantage of this powerful feature to improve your scripting skills and increase your productivity.
- Related Articles
- Bash trap Command Explained
- Bash Script for Loop Explained with Examples
- Array Operations in Linux bash
- How to Evaluate Arithmetic Expressions in Bash?
- Bash Printf - How to Print a Variable in Bash
- Bash-it – Bash Framework to Control Your Scripts and Aliases
- Bash Export Variable
- Bash String Comparison
- Bash Functions in Linux
- Bash Special Variables in Linux
- C vs BASH Fork bomb?
- Difference between Python and Bash
- Difference between CSH and BASH
- Bash HereDoc Tutorial With Examples
- Bash wait Command with Examples
