- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# How to Evaluate Arithmetic Expressions in Bash?

Bash is a powerful programming language used for writing shell scripts on Linux and other Unix-based systems. One of most common tasks in shell scripting is evaluating arithmetic expressions. In this article, we will discuss how to evaluate arithmetic expressions in Bash and explore some examples.

## Introduction

Arithmetic expressions are mathematical calculations performed on numerical values. In Bash, arithmetic expressions are evaluated using expr command, which evaluates a string as an arithmetic expression and returns result. syntax for expr command is as follows −

$ expr expression

Here, expression is arithmetic expression to be evaluated. For example, to evaluate expression 2 + 3, we would enter following command −

$ expr 2 + 3

This would return result 5.

## Basic Arithmetic Operators

Bash supports all basic arithmetic operators, including addition, subtraction, multiplication, and division. These operators can be used to perform simple arithmetic calculations. table below shows basic arithmetic operators and their corresponding symbols.

Operator |
Symbol |
---|---|

Addition |
+ |

Subtraction |
- |

Multiplication |
* |

Division |
/ |

Let's look at some examples of using these basic arithmetic operators in Bash.

### Example 1: Addition

To perform addition in Bash, we use + symbol. For example, to add 2 and 3, we would enter following command −

$ expr 2 + 3

This would return result 5.

### Example 2: Subtraction

To perform subtraction in Bash, we use - symbol. For example, to subtract 3 from 5, we would enter following command −

$ expr 5 - 3

This would return result 2.

### Example 3: Multiplication

To perform multiplication in Bash, we use * symbol. For example, to multiply 2 and 3, we would enter following command −

$ expr 2 * 3

Note that * symbol needs to be escaped with a backslash () to prevent it from being interpreted as a wildcard character by shell.

This would return result 6.

### Example 4: Division

To perform division in Bash, we use / symbol. For example, to divide 6 by 2, we would enter following command −

$ expr 6 / 2

This would return result 3.

### Order of Precedence

When evaluating arithmetic expressions in Bash, it is important to keep in mind order of precedence of arithmetic operators. order of precedence determines order in which operators are evaluated.

The order of precedence for basic arithmetic operators is as follows −

Multiplication and division (evaluated left to right)

Addition and subtraction (evaluated left to right)

For example, in expression 2 + 3 * 4, multiplication is evaluated first, and expression is evaluated as 2 + 12, which results in 14.

Let's look at some examples of using order of precedence in Bash.

### Example 5: Multiplication and Division

In expression 2 + 4 / 2 * 3, division is evaluated first, and expression is evaluated as 2 + 2 * 3, which results in 8. To evaluate expression as (2 + 4) / (2 * 3), we would use parentheses to group addition and multiplication −

$ expr 2+42+4 / 2\*32\*3

This would return result 1.

### Example 6: Grouping with Parentheses

To group parts of an expression together, we can use parentheses. For example, in expression 2 * 3 + 4, we can group multiplication with parentheses to ensure it is evaluated first −

$ expr 2 * 3+43+4

This would return result 14.

### Modulus Operator

In addition to basic arithmetic operators, Bash also supports modulus operator (%), which returns remainder of a division operation. For example, to calculate remainder when 5 is divided by 2, we would enter following command −

$ expr 5 % 2

This would return result 1.

Let's look at an example of using modulus operator in Bash.

### Example 7: Modulus Operator

In expression 17 % 4 + 3 * 2, modulus operation is evaluated first, and expression is evaluated as 1 + 6, which results in 7.

$ expr 17 % 4 + 3 * 2

This would return result 7.

## Advanced Arithmetic Functions in Bash

Bash also provides some advanced arithmetic functions that can be used in arithmetic expressions. These functions include square roots, exponents, and absolute values. Let's take a look at each of these functions in more detail.

### Square Roots

To calculate square root of a number in Bash, we use sqrt function. For example, to calculate square root of 16, we would enter following command −

$ expr sqrt 16

This would return result 4.

### Exponents

To raise a number to a power in Bash, we use ** operator. For example, to calculate 2 raised to power of 3, we would enter following command −

$ expr 2 ** 3

This would return result 8.

### Absolute Values

To calculate absolute value of a number in Bash, we use abs function. For example, to calculate absolute value of -5, we would enter following command −

$ expr abs -5

This would return result 5.

### Using Variables in Arithmetic Expressions

In Bash, we can also use variables in arithmetic expressions. We can assign values to variables using = operator, and then use variables in arithmetic expressions. For example, to assign value 5 to a variable named x, we would enter following command −

$ x=5

We can then use variable in an arithmetic expression. For example, to add 2 to value of x, we would enter following command −

$ expr $x + 2

This would return result 7.

## Conclusion

Evaluating arithmetic expressions is a common task in Bash scripting. By using expr command and basic arithmetic operators, as well as order of precedence and modulus operator, we can perform simple arithmetic calculations in Bash. With examples we've covered in this article, you should have a good understanding of how to evaluate arithmetic expressions in Bash and how to apply these concepts in your own scripts.