Arithmetic operators
Native bash does not support simple math operations, but can be implemented with other commands, such as awk and expr,expr, which are most commonly used.
Expr is an expression evaluation tool that uses it to perform evaluation operations on expressions.
Example: Implementing two number additions
#!/bin/bashval= 'expr22'echo'total value: ${ val}"
There are spaces between the expression and the operator , and each expression and operator can be seen as a parameter passed to expr
The following is a list of arithmetic operators, where multiplication sign (*) must have a backslash (\) in order to achieve multiplication
Relational operators
Relational operators only support numbers, and strings are not supported unless the value of the string is a number.
The following is a list of relational operators
An example of a relational operator is given below
#!/bin/sha=tenb=if [$a-eq $b] Then Echo " $a-eq $b: A is equal to B " Else Echo " $a-eq $b: A is not equal to B "
NOTE: Conditional expressions are placed between square brackets and have spaces
Boolean operator
The following is an example of a Boolean operator. Where -O is OR operation,-A is associated with the operation
#!/bin/SHa=Tenb= -if[$a! =$b] Then Echo "$a! = $b: equal to B"fiif[$a-lt --A $b-GT the ] Then Echo "$a-lt 100-a $b-gt 15:returns true"fiif[$a-lt --O $b-gt - ] Then Echo "$a-lt 100-o $b-gt 100:returns true"fi
String operators
The following is a list of string operators
The following is an example of a string operator
#!/bin/SHa="ABC"b="EFG"if[$a =$b] Then Echo "$a = $b: A is equal to B"Else Echo "$a = $b: A is not equal to B"if[ -Z $a] Then Echo "- Z $a: string length is zero"Else Echo "- Z $a: string length is not zero"fiif[ -N $a] Then Echo "- N $a: string length is not zero"Else Echo "- N $a: string length is zero"fiif[$a] Then Echo "$a: String is not empty"Else Echo "$a: string is empty"fi
File Test Operators
The following is a list of file test operators
The following is an example of a file test operator
#!/bin/bashfile="/home/runnyu/demo.c"if [-R $file ] then echo"File has Read access"else echo"File does not has read acces"fi
Shell Script value: operator