Bash is a powerful shell scripting language that provides a wide range of commands for various operations. One of the most important commands in Bash is the “expr” command, which is used for performing basic arithmetic operations and string manipulation. The “expr” command allows you to perform mathematical calculations, evaluate expressions, and manipulate strings in a Bash script.

The **“expr”** command is a part of the **coreutils** package, which is installed by default on most Unix-based systems, including Linux and macOS. It takes one or more arguments as input, and based on the type of input, it performs the appropriate operation. For example, if you pass two numbers as input, the “expr” command will perform a basic arithmetic operation like addition, subtraction, multiplication, or division, and return the result.

In this article, we will discuss a few practical examples of using the **“expr”** command in Bash scripts. These examples cover a wide range of use cases and will help you understand how to use the “expr” command effectively in your own scripts.

- Calculating the sum of two variables
- Subtracting two variables
- Multiplying two variables
- Dividing two variables
- Finding the remainder of two variables
- Incrementing a variable
- Decrementing a variable
- Calculating the average of a list of numbers

The following command calculates the sum of two variables and assigns the result to a third variable:

1 2 3 4 5 6 | a=10 b=20 c=`expr $a + $b` echo $c #Output: 30 |

The following command subtracts the value of the second variable from the first variable and assigns the result to a third variable:

1 2 3 4 5 6 | a=50 b=30 c=`expr $a - $b` echo $c #Output: 20 |

The following command multiplies two variables and assigns the result to a third variable:

1 2 3 4 | a=5 b=10 c=`expr $a \* $b` echo $c #Output: 50 |

The following command divides the value of the first variable by the second variable and assigns the result to a third variable:

1 2 3 4 | a=100 b=10 c=`expr $a / $b` echo $c #Output: 10 |

The following command finds the remainder of the division of two variables and assigns the result to a third variable:

1 2 3 4 | a=100 b=9 c=`expr $a % $b` echo $c #Output: 1 |

The following command increments the value of a variable by 1:

1 2 3 | a=10 a=`expr $a + 1` echo $a Output: 11 |

The following command decrements the value of a variable by 1:

1 2 3 | a=20 a=`expr $a - 1` echo $a #Output: 19 |

The following command calculates the average of a list of numbers:

1 2 3 4 5 6 7 8 9 | numbers="10 20 30 40 50" sum=0 count=0 for i in $numbers; do sum=`expr $sum + $i` count=`expr $count + 1` done average=`expr $sum / $count` echo $average |

Output: 30

## Conclusion

The **“expr”** command is a powerful tool for performing arithmetic and string operations in Bash scripts. With the examples presented in this article, you can see the wide range of use cases for the **“expr”** command. These examples cover some of the most common use cases, but there are many more ways to use the **“expr”** command in your Bash scripts. Whether you are a beginner or an experienced Bash user, understanding the “expr” command is essential for writing powerful and efficient Bash scripts.