Skip to content

Arithmetic operation in Bash

homepage-banner

Introduction

Bash is a popular shell used in Unix-based systems. It is used to execute commands and automate tasks. One of the useful features of Bash is the ability to perform arithmetic operations. In this blog post, we will discuss how to perform arithmetic operations in Bash.

Basic Arithmetic Operations

Bash supports all the basic arithmetic operations, such as addition, subtraction, multiplication, and division. The syntax for performing these operations is as follows:

1:$(( ))

2:$[ ]

3:expr

C=`expr $A + $B`

4:bc

#!/bin/bash
x=1
y=2
echo $(( x + y ))
echo $[ $x + $y ]
echo `expr $x + $y`
let z=$x+$y
echo $z

bc <<< "scale=3; 11/15"

5:let

let C=$A+$B

Conclusion

Arithmetic operations are an essential part of Bash scripting. In this blog post, we discussed how to perform basic arithmetic operations, the modulus operation, and increment and decrement operators. Understanding these operations will help you write more complex Bash scripts.

Leave a message