Arithmetics
Here we will learn how to perform arithmetic operations using shell scripts.
Arithmetics in Linux Shell Scripting
We use the keyword " expr " to perform arithmetic operations.
Syntax:
expr op1 math-operator op2
Example:
$ expr 3 + 2 $ expr 3 - 2 $ expr 10 / 2 $ expr 20 % 3 $ expr 10 \* 3 $ echo `expr 3 + 2`
In shell scripting we will be using the echo command to print the result of the arithmetic operations.
Note:
If you use echo command, before expr keyword we use ` (back quote) sign.
Here both double and single quote will not give you the desired result.
Try the following commands:
$ echo "expr 3 + 2" # Result expr 3 + 2 $ echo 'expr 3 + 2' # Result expr 3 + 2 $ echo `expr 3 + 2` # Result 5
From the above example, we see that if we use double or single quote, the echo command directly prints " expr 3 + 2 ".
To get the result of this expression, we need to use the back quote.
Related posts:
- Shell Scripting Introduction
- Shell Syntax
- Shell - Strings
- Shell - Variables
- Shell - User Intraction
- First Shell Script
- Shell - Arithmetics
- Shell - Case Statement
- Shell - Command Line Arguments
- Shell - For Loop
- Shell - While Loops
- Shell - IF Statements
- Shell - Input Output Redirections
- Shell - Pipes and Filters
- Shell - Process Management
Your IP address1 is: 172.18.0.2
Tutorials
Latest Updates
Follow us
- Tech-Tutorials twitter.com/techcuriosity