Magazine

Using the Expr Command for Arithmetic in Linux Shell

Posted on the 08 April 2021 by Satish Kumar @satish_kumar86

We can use theexprcommand forarithmeticoperations. Theexprcommand is an external command; the binary of theexprcommand isstoredin the folder called/usr/bin/expr.

Perform an addition operation as follows:

$ expr 40 + 242

Perform a subtraction operation as follows:

$ expr 42 - 240

Perform a division operation as follows:

$ expr 40 / 104

Perform a modulus (getting remainder) operation as follows:

$ expr 42 % 102$ expr 4 * 10expr: syntax error

With the expr command, we cannot use * for multiplication. We need to use * for multiplication:

$ expr "4 * 10"4 * 10$ expr 4 * 1040

We will write a simple script to add two numbers. Write the shell script, arithmetic_01.sh as follows:

arithmetic_01.sh
!/bin/bash 
x=5 
y=2 
z=`expr $x + $y` 
echo $z  

Test the script as follows:

$ chmod +x arithmetic_01.sh 
$ ./arithmetic_01.sh

This is the output:

Output:
7

Let’s write a script to perform all the basic arithmetic operations. Write the Shell script called arithmetic_02.sh as follows:

arithmetic_02.sh
#!/bin/bash 
var1=30 
var2=20 
echo `expr $var1 + $var2`        # Arithmetic Addition 
echo `expr $var1 - $var2`        # Arithmetic Subtraction 
echo `expr $var1 * $var2`       # Arithmetic Multiplication 
echo `expr $var1 / $var2`        # Arithmetic Division 
echo `expr $var1 % $var2`        # Arithmetic Modular Division 
                                 # (Remainder)

Let us test the script:

$ chmod +x arithmetic_02.sh
$ ./arithmetic_02.sh

This is the output:

Output:
5010600110

Using an arithmetic expansion

We can use two different ways for evaluating arithmetic expressions:

$(( expression ))
$[ expression ]

Learn arithmetic operations using the preceding mentioned arithmetic expansion:

$ a=10
$ b=20
$ c=$((a + b))
$ echo $c

During arithmetic operations, we may need to find the square or cube of any given number. These operations are called exponent operations. We can perform exponent operations as follows:

$ a=5
$ b=3
$ expo=$[ $a ** $b ]    # This is equivalent to ab
$ echo $expo
125

This is the result of the 53operations:

Another way to do arithmetic expansions is as follows:

$ B=10
$ A=$[B + 10]
$ echo $A20
$ echo $[ 3 + 4 - 5 ]2
$ echo $[ 3 + 4 * 5]23

Arithmetic multiplication has precedence over addition. Therefore, 4*5 was performed first, and the addition of 3+20 was performed later on:

$ echo $[(3 + 4) * 5]35
$ echo $(( 3 + 4 ))7
$ echo $(( 6 / 0 ))bash: 6/0: division by 0 ( error token is "0")

We will use many of the precedingarithmetictechniques for doing the same addition operation and check the result.

Let’s write an interactive script calledarithmetic_03.shas follows:

arithmetic_03.sh
#!/bin/bash 
echo "Enter first value" 
read number_1 
echo "Enter second value" 
read number_2 
total=`expr $number_1 + $number_2` 
echo $total 
sum=$(($number_1 + $number_2)) 
echo "sum is "$sum 
echo "Sum is $[ $number_1+$number_2 ]"

Let us test the script:

$ chmod +x arithmetic_03.sh
$ ./arithmetic_03.sh

Output:

Output:
Enter first value10
Enter second value515
Sum is 15
Sum is 15

The preceding Shell script shows that even if we use any of the previous techniques, the result remains the same.

Let’s write a shell script calledarithmetic_04.shas follows:

arithmetic_04.sh
#!/bin/bash 
# Interactive Shell Script Demonstrating Arithmetic Operators 
echo "Enter First value" 
read number_1 
echo "Enter Second value" 
read number_2 
echo $(($number_1 + $number_2)) 
echo $(($number_1 / $number_2)) # Division of two numbers

Let’s test the program as follows:

$ chmod +x arithmetic_04.sh
$ ./arithmetic_04.sh

This should produce the following output:

Output:
Enter First value10
Enter Second value5
15
2

We will write one more script with a different technique. Let’s write the Shell script arithmetic_05.sh as follows:

arithmetic_05.sh
#!/bin/bash 
# Script is For Demonstrating Arithmetic 
var1=10 
var2=20 
echo $(($var1+$var2))  # Adding Two Values 
echo $(($var1-$var2))  # Subtract Two Values 
echo $(($var1*$var2))  # Multiply Two Values 
echo $(($var1%$var2))  # Remainder

Let’s test the program here:

$ chmod +x arithmetic_05.sh
$ ./arithmetic_05.sh

This should produce the following output:

Output:
30
-10
200
10

We will write a script to add five numbers that are passed from a command line. Let’s write the Shell script, arithmetic_06.sh as follows:

arithmetic_06.sh
#!/bin/bash 
# Write a shell script which will receive 5 numbers from command line 
# and print their sum. 
echo "Sum of Five Numbers is:" $(($1 + $2 + $3 + $4 + $5))

Let’s test the program:

$ chmod +x arithmetic_06.sh
$ ./arithmetic_06.sh 10 20 30 40 50

This should produce the following output:

Output:
Sum of Five Numbers is: 150

Let’s write the Shell script, arithmetic_07.sh as follows for finding the cube, quotient, and remainder:

arithmetic_07.sh
#!/bin/bash 
 
x=99 
 
(( cube = x * x * x )) 
(( quotient = x / 5 )) 
(( remainder = x % 5 )) 
 
echo "The cube of $x is $cube." 
echo "The quotient of $x divided by 5 is $quotient." 
echo "The remainder of $x divided by 5 is $remainder." 
 
# Note the use of parenthesis to controlling arithmetic operator 
# precedence evaluation. 
(( y = 2 * (quotient * 5 + remainder) )) 
echo "Two times $x is $y."

Let’s test the program:

$ chmod +x arithmetic_07.sh
$ ./arithmetic_07.sh

This should produce the following output:

Output:
The cube of 99 is 970299.
The quotient of 99 divided by 5 is 19.
The remainder of 99 divided by 5 is 4.
Two times 99 is 198.

Back to Featured Articles on Logo Paperblog