Operators in shell script
Continuation “https://samhith1053.medium.com/basics-of-shell-scripting-b4d0701d68a0”
Operators in shell script:
Arithmetic operators:
Arithmetic operators in shell script are (Addition(+),Subtraction(-), Multiplication(*),Division(/),Modulus(%))
We can implement arithmetic operators in 2 ways
- Using subshell
- Using expr expression
Using Subshell:
Using read we read the input from the user
$(($a+$b)) — — it creates a subshell and compute the value of a+b and again return to the parent shell
Using expr expression:
Note: There should be space between the operand and operator while using expr with reverse quotations
Relational Operators:
The relational operators in shell script is as follows
-lt stands for less than
-le stands for less than or equal to
-gt stands for greater than
-ge stands for greater than or equal to
-eq stands for equal to
-ne stands for not equal to
Logical Operators:
Logical operators are used to validate multiple conditions in a statement.
The main logical operators are
AND denoted by ‘-a’ in shell script . It produces true only if all the conditions in a statement are true
OR denoted by ‘-o’ in shell script. It produces true if any conditions in a statement is true
NOT denoted by ‘-n in shell script. If the statement is true then not output will be false. If the statement is false then not produces true