Bash supports many operators, including Arithmetic Operators, Relational operators, boolean operators, string operators, and file test operators.
Native Bash does not support simple mathematical operations, but can be implemented using other commands, such as awk and expr, which are most commonly used.
Expr is an expression computing tool that can be used to evaluate an expression.
For example, add two numbers:
Copy text-only new window
#!/bin/bashval=`expr 2 + 2`echo "Total value : $val"
Run the Script output:
Total value : 4
Note:
- There must be spaces between expressions and operators. For example, 2 + 2 is incorrect and must be written as 2 + 2, which is different from most programming languages we are familiar.
- The complete expression must be included in ''. Note that this character is not a common single quotation mark, which is under the ESC key.
Here is an example of using arithmetic operators:
Copy text-only new window
#!/bin/sha=10b=20val=`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"fiif [ $a != $b ]then echo "a is not equal to b"fi
Running result:
a + b : 30a - b : -10a * b : 200b / a : 2b % a : 0a is not equal to b
Note:
- A backslash (\) must be added to the front side of the multiplication sign (*) to realize multiplication;
- If... then... Fi is a conditional statement, which will be explained later.
Arithmetic Operator list
Operator |
Description |
Example |
+ |
Addition |
'Expr $ A + $ B 'returns 30. |
- |
Subtraction |
'Expr $ A-$ B 'returns 10. |
* |
Multiplication |
'Expr $ A \ * $ B 'returns 200. |
/ |
Division |
'Expr $ B/$ a' returns 2. |
% |
Remainder |
The 'expr $ B % $ a' result is 0. |
= |
Assignment |
A = $ B assigns the value of variable B to. |
= |
Equal. Used to compare two numbers. If the two numbers are the same, true is returned. |
[$ A = $ B] returns false. |
! = |
Not equal. Used to compare two numbers. If they are different, true is returned. |
[$! = $ B] returns true. |
Note: The conditional expression must be placed between square brackets with spaces. For example, [$ A = $ B] is incorrect and must be written as [$ A = $ B]. Relational operators only support numbers, not strings, unless the value of a string is a number.
Let's look at an example of a relational OPERATOR:
Copy text-only new window
#!/bin/sha=10b=20if [ $a -eq $b ]then echo "$a -eq $b : a is equal to b"else echo "$a -eq $b: a is not equal to b"fiif [ $a -ne $b ]then echo "$a -ne $b: a is not equal to b"else echo "$a -ne $b : a is equal to b"fiif [ $a -gt $b ]then echo "$a -gt $b: a is greater than b"else echo "$a -gt $b: a is not greater than b"fiif [ $a -lt $b ]then echo "$a -lt $b: a is less than b"else echo "$a -lt $b: a is not less than b"fiif [ $a -ge $b ]then echo "$a -ge $b: a is greater or equal to b"else echo "$a -ge $b: a is not greater or equal to b"fiif [ $a -le $b ]then echo "$a -le $b: a is less or equal to b"else echo "$a -le $b: a is not less or equal to b"fi
Running result:
10 -eq 20: a is not equal to b10 -ne 20: a is not equal to b10 -gt 20: a is not greater than b10 -lt 20: a is less than b10 -ge 20: a is not greater or equal to b10 -le 20: a is less or equal to b
Relational operators
Operator |
Description |
Example |
-EQ |
Returns true if the two numbers are equal. |
[$ A-EQ $ B] returns true. |
-Ne |
Checks whether two numbers are equal. If they are not equal, true is returned. |
[$ A-ne $ B] returns true. |
-GT |
Checks whether the number on the left is greater than the number on the right. If yes, returns true. |
[$ A-GT $ B] returns false. |
-Lt |
Checks whether the number on the left is smaller than the number on the right. If yes, returns true. |
[$ A-lt $ B] returns true. |
-Ge |
Check whether the number on the left is equal to the value on the right. If yes, true is returned. |
[$ A-ge $ B] returns false. |
-Le |
Checks whether the number on the left is less than or equal to the number on the right. If yes, returns true. |
[$ A-Le $ B] returns true. |
Here is an example of a boolean operator:
Copy text-only new window
#!/bin/sha=10b=20if [ $a != $b ]then echo "$a != $b : a is not equal to b"else echo "$a != $b: a is equal to b"fiif [ $a -lt 100 -a $b -gt 15 ]then echo "$a -lt 100 -a $b -gt 15 : returns true"else echo "$a -lt 100 -a $b -gt 15 : returns false"fiif [ $a -lt 100 -o $b -gt 100 ]then echo "$a -lt 100 -o $b -gt 100 : returns true"else echo "$a -lt 100 -o $b -gt 100 : returns false"fiif [ $a -lt 5 -o $b -gt 100 ]then echo "$a -lt 100 -o $b -gt 100 : returns true"else echo "$a -lt 100 -o $b -gt 100 : returns false"fi
Running result:
10 != 20 : a is not equal to b10 -lt 100 -a 20 -gt 15 : returns true10 -lt 100 -o 20 -gt 100 : returns true10 -lt 5 -o 20 -gt 100 : returns false
Boolean operator list
Operator |
Description |
Example |
! |
Non-operation. If the expression is true, false is returned. Otherwise, true is returned. |
[! False] returns true. |
-O |
Or operation. If one expression is true, true is returned. |
[$ A-lt 20-o $ B-GT 100] returns true. |
- |
Returns true only when both expressions are true. |
[$ A-lt 20-a $ B-GT 100] false is returned. |
Here is an example of a string OPERATOR:
Copy text-only new window
#!/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"fiif [ $a != $b ]then echo "$a != $b : a is not equal to b"else echo "$a != $b: a is equal to b"fiif [ -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
Running result:
abc = efg: a is not equal to babc != efg : a is not equal to b-z abc : string length is not zero-n abc : string length is not zeroabc : string is not empty
String operator list
Operator |
Description |
Example |
= |
Returns true if two strings are equal. |
[$ A = $ B] returns false. |
! = |
Returns true if the two strings are equal or not. |
[$! = $ B] returns true. |
-Z |
Checks whether the string length is 0. If it is 0, true is returned. |
[-Z $ A] returns false. |
-N |
Checks whether the string length is 0. If it is not 0, true is returned. |
[-Z $ A] returns true. |
Str |
Checks whether the string is null. If it is not null, true is returned. |
[$ A] returns true. |
File test operator file test operator is used to detect various properties of UNIX files.
For example, the variable file indicates the file "/var/www/tutorialspoint/Unix/test. Sh", which is 100 bytes in size and has the rwx permission. The following code detects various attributes of the file:
Copy text-only new window
#!/bin/shfile="/var/www/tutorialspoint/unix/test.sh"if [ -r $file ]then echo "File has read access"else echo "File does not have read access"fiif [ -w $file ]then echo "File has write permission"else echo "File does not have write permission"fiif [ -x $file ]then echo "File has execute permission"else echo "File does not have execute permission"fiif [ -f $file ]then echo "File is an ordinary file"else echo "This is sepcial file"fiif [ -d $file ]then echo "File is a directory"else echo "This is not a directory"fiif [ -s $file ]then echo "File size is zero"else echo "File size is not zero"fiif [ -e $file ]then echo "File exists"else echo "File does not exist"fi
Running result:
File has read accessFile has write permissionFile has execute permissionFile is an ordinary fileThis is not a directoryFile size is zeroFile exists
File test operator list
Operator |
Description |
Example |
-B file |
Checks whether the file is a block device file. If yes, returns true. |
[-B $ file] returns false. |
-C file |
Checks whether the file is a character device file. If yes, returns true. |
[-B $ file] returns false. |
-D File |
Checks whether the file is a directory. If yes, returns true. |
[-D $ file] returns false. |
-F File |
Checks whether a file is a common file (neither a directory nor a device file). If yes, returns true. |
[-F $ file] returns true. |
-G File |
Checks whether the sgid bit is set for the file. If yes, true is returned. |
[-G $ file] returns false. |
-K file |
Check whether Sticky Bit is set in the file. If yes, true is returned. |
[-K $ file] returns false. |
-P file |
Checks whether the file is a named pipe. If yes, returns true. |
[-P $ file] returns false. |
-U File |
Check whether the SUID bit is set for the file. If yes, true is returned. |
[-U $ file] returns false. |
-R File |
Checks whether the file is readable. If yes, true is returned. |
[-R $ file] returns true. |
-W File |
Checks whether the file is writable. If yes, true is returned. |
[-W $ file] returns true. |
-X file |
Checks whether the file is executable. If yes, true is returned. |
[-X $ file] returns true. |
-S file |
Check whether the file is empty (whether the file size is greater than 0). If it is not empty, true is returned. |
[-S $ file] returns true. |
-E File |
Checks whether a file (including directories) exists. If yes, returns true. |
[-E $ file] returns true. |
Copy and format a new window
Shell tutorial 5-shell Operators