domingo, 22 de mayo de 2022

Performing Arithmetic Operations in Bash

https://www.javatpoint.com/bash-arithmetic-operators

Performing Arithmetic Operations in Bash


There are many options to perform arithmetic operations on the bash shell. Some of the options are given below that we can adopt to perform arithmetic operations:

Double Parentheses

Double parentheses is the easiest mechanism to perform basic arithmetic operations in the Bash shell. We can use this method by using double brackets with or without a leading $.

Syntax

  1. ((expression))  

We can apply four different ways to achieve the required objectives. Look at the methods given below to understand how the double parentheses mechanism can be used (Assuming that we want to add the numbers 10 and 3) :

Method 1

  1. Sum=$((10+3))  
  2. echo "Sum = $Sum"  

Method 2

  1. ((Sum=10+3))  
  2. echo "Sum = $Sum"  

Method 3

  1. Num1=10  
  2. Num2=3  
  3. ((Sum=Num1+Num2))  
  4. echo "Sum = $Sum"  

Method 4

  1. Num1=10  
  2. Num2=3  
  3. Sum=$((Num1+Num2))  
  4. echo "Sum = $Sum"  

All of these methods will provide the same output as:

Sum = 13

Following is an example demonstrating the use of double parentheses for arithmetic operations in a Bash shell script:

Bash Script

  1. #!/bin/bash  
  2.   
  3. x=8  
  4. y=2  
  5. echo "x=8, y=2"  
  6. echo "Addition of x & y"  
  7. echo $(( $x + $y ))  
  8. echo "Subtraction of x & y"  
  9. echo $(( $x - $y ))  
  10. echo "Multiplication of x & y"  
  11. echo $(( $x * $y ))  
  12. echo "Division of x by y"  
  13. echo $(( $x / $y ))  
  14. echo "Exponentiation of x,y"  
  15. echo $(( $x ** $y ))  
  16. echo "Modular Division of x,y"  
  17. echo $(( $x % $y ))  
  18. echo "Incrementing x by 5, then x= "  
  19. (( x += 5 ))   
  20. echo $x  
  21. echo "Decrementing x by 5, then x= "  
  22. (( x -= 5 ))  
  23. echo $x  
  24. echo "Multiply of x by 5, then x="  
  25. (( x *= 5 ))  
  26. echo $x  
  27. echo "Dividing x by 5, x= "  
  28. (( x /= 5 ))  
  29. echo $x  
  30. echo "Remainder of Dividing x by 5, x="  
  31. (( x %= 5 ))  
  32. echo $x  

Output

Bash Arithmetic Operators

Let Construction

Let is a built-in command of Bash that allows us to perform arithmetic operations. It follows the basic format:

Syntax

  1. let <arithmetic expression>  

An example is given below explaining how to use let command in a Bash script:

Bash script

  1. #!/bin/bash  
  2.   
  3. x=10  
  4. y=6  
  5. z=0  
  6. echo "Addition"  
  7. let "z = $(( x + y ))"  
  8. echo "z= $z"  
  9.   
  10. echo "Substraction"  
  11. let "z = $((x - y ))"  
  12. echo "z= $z"  
  13.   
  14. echo "Multiplication"  
  15. let "z = $(( x * y ))"  
  16. echo "z = $z"  
  17.   
  18. echo "Division"  
  19. let "z = $(( x / y ))"  
  20. echo "z = $z"  
  21.   
  22. echo "Exponentiation"  
  23. let "z = $(( x ** y ))"  
  24. echo "z = $z"  
  25.   
  26. echo "Modular Division"  
  27. let "z = $(( x % y ))"  
  28. echo "z = $z"  
  29.   
  30. let "x += 5"  
  31. echo "Incrementing x by 5, then x= "  
  32. echo $x  
  33.   
  34. let "x -= 5"  
  35. echo "Decrementing x by 5, then x= "  
  36. echo $x  
  37.   
  38. let "x *=5"  
  39. echo "Multiply of x by 5, then x="  
  40. echo $x  
  41.   
  42. let "x /= 5"  
  43. echo "Dividing x by 5, x= "  
  44. echo $x  
  45.   
  46. let "x %= 5"  
  47. echo "Remainder of Dividing x by 5, x="  
  48. echo $x  

Output

Bash Arithmetic Operators

Backticks

In bash scripting, an arithmetic expansion can also be performed using backticks and expr (known as all-purpose expression evaluator). The `expr` is similar to 'let,' but it does not save the result to a variable. It directly prints the result. Unlike let, we don't need to enclose the expression within the quotes. We are required to use spaces between the items of the expression. It is important to note that we should use 'expr` within command substitution to save the output to a variable.

We can also use `expr` without 'backticks'.

Syntax

  1. `expr item1 operator item2`  
  2. or  
  3. expr item1 operator item2  

An example is given below explaining how to use backticks and expr in a Bash script:

Bash Script Program

  1. #!/bin/bash  
  2. #Basic arithmetic using expr  
  3.   
  4. echo "a=10, b=3"  
  5. echo "c is the value of addition c=a+b"  
  6. a=10  
  7. b=3  
  8. echo "c= `expr $a + $b`"  

Output

Bash Arithmetic Operators

Conclusion

In this topic, we discussed how to use arithmetic operators to perform arithmetic operations.

No hay comentarios:

Publicar un comentario