Bash provides the ability to perform basic math operations directly from the terminal. This allows you to easily manipulate numbers and perform calculations without the need for a separate calculator program.
In this article, we will explore some of the basic math operations you can perform using Bash.
Addition
To add two numbers together in Bash, you can use the + operator.
Example:
1#!/bin/bash
2num1=5
3num2=10
4sum=$((num1 + num2))
5echo "The sum is: $sum"
In this example, we declare two variables num1 and num2 with values 5 and 10 respectively. We then use the $(( ... )) syntax to perform the addition operation and assign the result to a variable sum. Finally, we use echo to display the result.
Output:
The sum is: 15
Subtraction
Similarly, you can subtract one number from another using the - operator.
Example:
1#!/bin/bash
2num1=15
3num2=8
4difference=$((num1 - num2))
5echo "The difference is: $difference"
Multiplication
To multiply two numbers in Bash, you can use the * operator.
1#!/bin/bash
2num1=7
3num2=3
4product=$((num1 * num2))
5echo "The product is: $product"
Division
Bash also supports division operations using the / operator. However, bash only performs integer division by default. If you want to perform floating-point division, you need to use an external tool like Basic Calculator (bc).
1#!/bin/bash
2num1=10
3num2=2
4quotient=$((num1 / num2))
5echo "The quotient is: $quotient"
Using bc tool
To install bc, use below command.
sudo apt install bc
This tool can be used interactively like below.
bc
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
3 + 4
7
5 / 2
2
5 % 2
1
quit
If we want to use calculations like these in our script then we have to pipe these operations to bc command.
1#!/bin/bash
2echo '5/2' | bc
3modulo=$(echo '5 % 2' | bc)
4echo "5 % 2 = $modulo"
Output:
2
5 % 2 = 1
Performing basic math operations in Bash can simplify your scripting tasks and allow you to quickly perform calculations without leaving the terminal. By mastering these simple operations, you can build more complex scripts that involve mathematical computations.


Comments