Shell Basic Operators
The Shell, like other programming languages, supports a variety of operators, including:
- Relational operators
- Boolean operator
- String operators
- File Test 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.
Cases:
Val= ' Expr 2 + 2 ' echo "Sum of two numbers: $val" Output: The sum of the two numbers is: 4 |
Arithmetic operators
The following table lists the commonly used arithmetic operators, assuming that variable A is 10 and variable B is 20:
Operator |
Description |
Example |
+ |
Addition |
Expr $a + $b ' result is 30. |
- |
Subtraction |
Expr $a-$b ' result is-10. |
* |
Multiplication |
The ' expr $a \* $b ' result is 200. |
/ |
Division |
The ' expr $b/$a ' result is 2. |
% |
Take surplus |
The ' expr $b% $a ' result is 0. |
= |
Assign value |
A= $b assigns the value of variable B to a. |
== |
Equal. Used to compare two numbers, the same returns true. |
[$a = = $b] returns FALSE. |
!= |
Not equal. Used to compare two numbers, not the same returns true |
[$a! = $b] Returns TRUE. |
Cases:
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 equals B" Fi if [$a! = $b] Then echo "A is not equal to B" Fi Output: A + b:30 A-B: 10 A * b:200 B/A: 2 B% a:0 A is not equal to B |
Attention:
- Multiplication sign (*) must be added in front of the backslash (\) to achieve the multiplication operation;
- If...then...fi is a conditional statement that will be explained later.
- The expr syntax for the shell in MAC is:$ ( expression ), where "*" in the expression does not need to escape the symbol "\".
|
Relational operators
Relational operators only support numbers, and strings are not supported unless the value of the string is a number. The following table lists the commonly used relational operators, assuming that variable A is 10 and variable B is 20:
-eq |
Detects whether two numbers are equal and returns true for equality. |
[$a-eq $b] returns false |
-gt |
Detects if the number on the left is greater than the right and, if so, returns True. |
[$a-gt $b] returns false |
-lt |
Detects if the number on the left is less than the right and, if so, returns True. |
[$a-lt $b] returns True |
-ge |
Detects if the left number is greater than or equal to the right, and returns true if it is |
[$a-ge $b] returns false |
-le |
Detects if the left number is less than or equal to the right, and returns true if it is. |
[$a-le $b] returns TRUE. |
Cases:
a=10
B=20
If [$a-eq $b]
Then
echo "$a-eq $b: a equals B"
Else
echo "$a-eq $b: A is not equal to B"
Fi
If [$a-ne $b]
Then
echo "$a-ne $b: A is not equal to B"
Else
echo "$a-ne $b: a equals B"
Fi
If [$a-gt $b]
Then
echo "$a-gt $b: a greater than B"
Else
echo "$a-gt $b: A is not much more than B"
Fi
If [$a-lt $b]
Then
echo "$a-lt $b: a less than B"
Else
echo "$a-lt $b: A is not less than B"
Fi
If [$a-ge $b]
Then
echo "$a-ge $b: A is greater than or equal to B"
Else
echo "$a-ge $b: a less than B"
Fi
If [$a-le $b]
Then
echo "$a-le $b: A is less than or equal to B"
Else
echo "$a-le $b: a greater than B"
Fi
Output: 10-eq 20:a Not equal to B
10-ne 20:a Not equal to B
10-GT 20:a is not much more than B.
10-lt 20:a less than B
10-ge 20:a less than B
10-le 20:a less than or equal to B
|
Boolean operator
The following table lists the commonly used Boolean operators, assuming that variable A is 10 and variable B is 20:
! |
Non-operation, the expression is true returns False, otherwise true. |
[! false] returns TRUE. |
-O |
Or operation, there is an expression of true to return true. |
[$a-lt 20-o $b-GT 100] returns TRUE. |
-A |
With an operation, two expressions are true to return true. |
[$a-lt 20-a $b-GT 100] returns FALSE. |
Cases
a=10
B=20
if [$a! = $b]
Then
echo "$a! = $b: A is not equal to B"
Else
echo "$a! = $b: a equals B"
Fi
If [$a-lt 100-a $b-gt 15]
Then
echo "$a less than 100 and $b greater than 15: Returns True"
Else
echo "$a less than 100 and $b greater than 15: returns false"
Fi
If [$a-lt 100-o $b-GT 100]
Then
echo "$a less than 100 or $b greater than 100: Returns True"
Else
echo "$a less than 100 or $b greater than 100: returns false"
Fi
If [$a-lt 5-o $b-GT 100]
Then
echo "$a less than 5 or $b greater than 100: Returns True"
Else
echo "$a less than 5 or $b greater than 100: returns false"
Fi
Output: Ten! = 20:a Not equal to B
10 is less than 100 and 20 is greater than 15: Returns True
10 is less than 100 or 20 is greater than 100: Returns True
10 less than 5 or 20 greater than 100: returns false
|
Logical operators:
&& |
Logical AND |
[[$a-lt && $b-GT 100]] returns false |
|| |
Logical OR |
[[$a-lt | | $b-GT 100]] returns True |
Cases:
a=10 B=20 if [[$a-lt && $b-GT 100]] Then echo "Return True" Else echo "return False" Fi if [[$a-lt | | $b-GT 100]] Then echo "Return True" Else echo "return False" Fi Output: Returns false Returns True |
String operators
The following table lists the commonly used string operators, assuming that variable a is "ABC" and Variable B is "EFG":
= |
Detects whether two strings are equal and returns true for equality. |
[$a = $b] returns FALSE. |
!= |
Detects whether two strings are equal and returns true if they are not equal. |
[$a! = $b] Returns TRUE. |
-Z Str |
Detects whether the string length is 0 and returns true for 0. Detects whether the string is empty and does not return true for null. |
[-Z $a] returns false. [$a] returns TRUE. |
Cases:
A= "ABC"
b= "EFG"
if [$a = $b]
Then
echo "$a = $b: a equals B"
Else
echo "$a = $b: A is not equal to B"
Fi
if [$a! = $b]
Then
echo "$a! = $b: A is not equal to B"
Else
echo "$a! = $b: a equals B"
Fi
If [-Z $a]
Then
echo "-Z $a: string length 0"
Else
echo "-Z $a: string length is not 0"
Fi
If [-N $a]
Then
echo "-N $a: string length not 0"
Else
echo "-N $a: string length 0"
Fi
If [$a]
Then
echo "$a: String not empty"
Else
echo "$a: string is empty"
Fi
Output: ABC = Efg:a Not equal to B
ABC! = EFG:A Not equal to B
-Z ABC: string length is not 0
-N ABC: string length is not 0
ABC: string is not empty
|
File Test operators:
File test operators are used to detect various properties of Unix files
-B File |
Detects if the file is a block device file, and returns True if it is. |
[-B $file] returns FALSE. |
-C file |
Detects if the file is a character device file, and returns True if it is. |
[-C $file] returns false. |
-D File |
Detects if the file is a directory, and returns True if it is. |
[-D $file] returns false. |
-F File |
Detects if the file is a normal file (neither a directory nor a device file), and returns True if it is. |
[-F $file] returns TRUE. |
-G file |
Detects if the file has a SGID bit set, and returns True if it is. |
[-G $file] returns false. |
-K File |
Detects if the file has a sticky bit set (Sticky bit), and returns True if it is. |
[-K $file] returns false. |
-P File |
Detects if the file is a well-known pipe, and returns True if it is. |
[-P $file] returns false. |
-U file |
Detects if the file has a SUID bit set, and returns True if it is. |
[-U $file] returns false. |
-R File |
Detects if the file is readable and returns true if it is |
[-R $file] returns true |
-W File |
Detects if the file is writable and returns true if it is. |
[-W $file] returns TRUE. |
-X File |
Detects if the file is executable and returns true if it is |
[-X $file] returns TRUE. |
-S file |
Detects whether the file is empty (the file size is greater than 0) and does not return true for null. |
[-S $file] returns TRUE. |
-E File |
Detects whether the file (including the directory) exists and, if so, returns True. |
[-e $file] returns TRUE. |
Cases:
File= "/var/www/runoob/test.sh"
If [-R $file]
Then
echo "File readable"
Else
echo "File not readable"
Fi
If [w $file]
Then
echo "File writable"
Else
echo "File not writable"
Fi
If [-X $file]
Then
echo "File Executable"
Else
echo "File not executable"
Fi
If [-f $file]
Then
echo "File as normal file"
Else
echo "File as special file"
Fi
If [-D $file]
Then
echo "File is a directory"
Else
echo "File is not a directory"
Fi
If [-s $file]
Then
echo "File is not empty"
Else
echo "File is empty"
Fi
If [-e $file]
Then
echo "File exists"
Else
echo "file does not exist"
Fi
Output: File readable
File can be written
File executable
File is a normal file
File is not a directory
The file is not empty
File exists
|
Shell Basic Operators