0% found this document useful (0 votes)
44 views

Arithmetic Operators: #!/bin/sh

This document discusses operators in the Bourne shell, including arithmetic, relational, boolean, string, and file test operators. It provides examples of using the expr command to perform arithmetic operations like addition and subtraction. Relational operators like == and != are used to compare values. All conditional expressions using these operators must be enclosed in square braces with spaces for accurate evaluation in the shell.

Uploaded by

rahul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Arithmetic Operators: #!/bin/sh

This document discusses operators in the Bourne shell, including arithmetic, relational, boolean, string, and file test operators. It provides examples of using the expr command to perform arithmetic operations like addition and subtraction. Relational operators like == and != are used to compare values. All conditional expressions using these operators must be enclosed in square braces with spaces for accurate evaluation in the shell.

Uploaded by

rahul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

There are various operators supported by each shell.

We will discuss in detail about


Bourne shell (default shell) in this chapter.
We will now discuss the following operators −

 Arithmetic Operators
 Relational Operators
 Boolean Operators
 String Operators
 File Test Operators
Bourne shell didn't originally have any mechanism to perform simple arithmetic
operations but it uses external programs, either awk or expr.
The following example shows how to add two numbers −
Live Demo
#!/bin/sh

val=`expr 2 + 2`
echo "Total value : $val"

The above script will generate the following result −


Total value : 4
The following points need to be considered while adding −
 There must be spaces between operators and expressions. For example, 2+2
is not correct; it should be written as 2 + 2.
 The complete expression should be enclosed between ‘ ‘, called the backtick.

Arithmetic Operators
The following arithmetic operators are supported by Bourne Shell.
Assume variable a holds 10 and variable b holds 20 then −
Show Examples

Operator Description Example

+ (Addition) Adds values on either side of the `expr $a + $b` will


operator give 30

- (Subtraction) Subtracts right hand operand from left `expr $a - $b` will give
hand operand -10
* Multiplies values on either side of the `expr $a \* $b` will
(Multiplication) operator give 200

/ (Division) Divides left hand operand by right hand `expr $b / $a` will give
operand 2

% (Modulus) Divides left hand operand by right hand `expr $b % $a` will
operand and returns remainder give 0

= (Assignment) a = $b would assign


Assigns right operand in left operand
value of b into a

== (Equality) Compares two numbers, if both are [ $a == $b ] would


same then returns true. return false.

!= (Not Compares two numbers, if both are [ $a != $b ] would


Equality) different then returns true. return true.

It is very important to understand that all the conditional expressions should be


inside square braces with spaces around them, for example [ $a == $b ] is correct
whereas, [$a==$b] is incorrect.
All the arithmetical calculations are done using long integers.

You might also like