
- Unix / Linux for Beginners
- Unix / Linux - Home
- Unix / Linux - Getting Started
- Unix / Linux - File Management
- Unix / Linux - Directories
- Unix / Linux - File Permission
- Unix / Linux - Environment
- Unix / Linux - Basic Utilities
- Unix / Linux - Pipes & Filters
- Unix / Linux - Processes
- Unix / Linux - Communication
- Unix / Linux - The vi Editor
- Unix / Linux Shell Programming
- Unix / Linux - Shell Scripting
- Unix / Linux - What is Shell?
- Unix / Linux - Using Variables
- Unix / Linux - Special Variables
- Unix / Linux - Using Arrays
- Unix / Linux - Basic Operators
- Unix / Linux - Decision Making
- Unix / Linux - Shell Loops
- Unix / Linux - Loop Control
- Unix / Linux - Shell Substitutions
- Unix / Linux - Quoting Mechanisms
- Unix / Linux - IO Redirections
- Unix / Linux - Shell Functions
- Unix / Linux - Manpage Help
- Advanced Unix / Linux
- Unix / Linux - Regular Expressions
- Unix / Linux - File System Basics
- Unix / Linux - User Administration
- Unix / Linux - System Performance
- Unix / Linux - System Logging
- Unix / Linux - Signals and Traps
- Unix / Linux Useful Resources
- Unix / Linux - Questions & Answers
- Unix / Linux - Useful Commands
- Unix / Linux - Quick Guide
- Unix / Linux - Builtin Functions
- Unix / Linux - System Calls
- Unix / Linux - Commands List
- Unix / Linux - Useful Resources
- Unix / Linux - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Unix / Linux - Shell Arithmetic Operators Example
The following arithmetic operators are supported by Bourne Shell.
Assume variable a holds 10 and variable b holds 20 then −
Operator | Description | Example |
---|---|---|
+ (Addition) | Adds values on either side of the operator | `expr $a + $b` will give 30 |
- (Subtraction) | Subtracts right hand operand from left hand operand | `expr $a - $b` will give -10 |
* (Multiplication) | Multiplies values on either side of the operator | `expr $a \* $b` will give 200 |
/ (Division) | Divides left hand operand by right hand operand | `expr $b / $a` will give 2 |
% (Modulus) | Divides left hand operand by right hand operand and returns remainder | `expr $b % $a` will give 0 |
= (Assignment) | Assigns right operand in left operand | a = $b would assign value of b into a |
== (Equality) | Compares two numbers, if both are same then returns true. | [ $a == $b ] would return false. |
!= (Not Equality) | Compares two numbers, if both are different then returns true. | [ $a != $b ] would 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.
Example
Here is an example which uses all the arithmetic operators −
#!/bin/sh a=10 b=20 val=`expr $a + $b` echo "a + b : $val" val=`expr $a - $b` echo "a - b : $val" val=`expr $a \* $b` echo "a * b : $val" val=`expr $b / $a` echo "b / a : $val" val=`expr $b % $a` echo "b % a : $val" if [ $a == $b ] then echo "a is equal to b" fi if [ $a != $b ] then echo "a is not equal to b" fi
The above script will produce the following result −
a + b : 30 a - b : -10 a * b : 200 b / a : 2 b % a : 0 a is not equal to b
The following points need to be considered when using the Arithmetic Operators −
There must be spaces between the operators and the expressions. For example, 2+2 is not correct; it should be written as 2 + 2.
Complete expression should be enclosed between ‘ ‘, called the inverted commas.
You should use \ on the * symbol for multiplication.
if...then...fi statement is a decision-making statement which has been explained in the next chapter.