Shell Initial (2) Shell Operator
Shell basic operator
Shell, like other programming languages, supports a variety of operators, including:
Arithmetic operator
Relational operator
Boolean operator
String operator
Native bash does not support simple mathematical operations, but can be implemented by other commands, such as awk and expr, expr is the most commonly used.
Such as:
val=`expr 2 + 3`
echo $val
Output: 5
Note that there needs to be a space between 2 + 3
Arithmetic operator
a variable is 10, b variable is 20.
operator | Explain | Give an example |
---|---|---|
+ | Plus | ` expr $a + B `, result: 30 |
- | Minus sign | ` expr $a - $b', result: - 10 |
* | Multiplication sign | ` expr $a * $b `, result: 200 |
/ | Division sign | ` Expr $b / a `, result: 2 |
% | Remainder | ` expr $b% $a `, result: 0 |
= | assignment | var=$a, assign variable a to var |
== | Be equal to | [$a == $b], the result is 0 |
!= | Not equal to | [$a!= $b], the result is 1 |
eg:
[zhang@localhost home]$ a=10
[zhang@localhost home]$ b=20
[zhang@localhost home]$ echo $a + $b = `expr $a + $b`
10 + 20 = 30
[zhang@localhost home]$ echo $a - $b = `expr $a - $b`
10 - 20 = -10
[zhang@localhost home]$ echo $a \* $b = `expr $a \* $b`
10 * 20 = 200
[zhang@localhost home]$ echo $a / $b = `expr $a / $b`
10 / 20 = 0
[zhang@localhost home]$ echo $b / $a = `expr $b / $a`
20 / 10 = 2
[zhang@localhost home]$ echo $b % $a = `expr $b % $a`
20 % 10 = 0
[zhang@localhost home]$ var=$a
[zhang@localhost home]$ echo $var
10
[zhang@localhost home]$ echo $[$a == $b]
0
[zhang@localhost home]$ echo $[$a != $b]
1
[zhang@localhost home]$ echo $[$a == 10]
1
Note: $[$a == $b] can be replaced by $[a == b], such as:
[zhang@localhost ~]$ a=10
[zhang@localhost ~]$ b=20
[zhang@localhost ~]$ echo $[a + b]
30
[zhang@localhost ~]$ echo $[a - b]
-10
[zhang@localhost ~]$ echo $[a * b]
200
[zhang@localhost ~]$ echo $[a / b]
0
[zhang@localhost ~]$ echo $[a == b]
0
[zhang@localhost ~]$ echo $[a != b]
1
[zhang@localhost ~]$ echo $[a >= b]
0
[zhang@localhost ~]$ echo $[a <= b]
1
[zhang@localhost ~]$ echo $[a % b]
10
Relational operator
operator | Explain | Give an example |
---|---|---|
-eq | Check if the two numbers are equal and return true equally. | [$a-eq $b] returns false |
-ne | Check whether the two numbers are not equal and return true equally. | [$a-ne $b] Returns true |
-gt | Check if the number on the left is greater than that on the right, and if so, return true. | [$a-gt $b] Returns false |
-lt | Check if the number on the left is less than the number on the right, and if so, return true. | [$a-lt $b] Returns true |
-ge | Check if the number on the left is greater than or equal to the number on the right, and if so, return true. | [$a-ge $b] returns false |
-le | Check if the number on the left is less than or equal to the number on the right, and if so, return true. | [$a-le $b] Returns true |
eg:
#!/bin/bash
a=10
b=20
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"
fi
if [ $a -ne $b ]
then
echo "$a -ne $b : a is not equal to b"
else
echo "$a -ne $b : a is equal to b"
fi
if [ $a -gt $b ]
then
echo "$a -gt $b : a is greater than b"
else
echo "$a -gt $b : a is not greater than b"
fi
if [ $a -lt $b ]
then
echo "$a -lt $b : a is 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 or equal to b"
else
echo "$a -ge $b : a is not greater or equal to b"
fi
if [ $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
Output results:
10 -eq 20: a is not equal to b
10 -ne 20 : a is not equal to b
10 -gt 20 : a is not greater than b
10 -lt 20 : a is less than b
10 -ge 20 : a is not greater or equal to b
10 -le 20 : a is less or equal to b
Boolean operator
operator | Explain | Give an example |
---|---|---|
! | If the expression is true, then false is returned, otherwise true is returned. | [! false] Returns true |
-o | Or an operation that returns true if an expression is true | [$a-lt 20-o $b-gt 100], returns true |
-a | With the operation, both expressions are true before returning true. | [$a-lt 20-a $b-gt 100], returns false |
eg:
#!/bin/bash
a=10
b=20
if [ $a != $b ]
then
echo "$a != $b"
else
echo "$a == $b"
fi
if [ $a -lt 20 -o $b -gt 100 ]
then
echo "$a less than 20 or $b greater than 100 : return true"
else
echo "$a less than 20 or $b greater than 100 : return false"
fi
if [ $a -lt 20 -a $b -gt 100 ]
then
echo "$a less than 20 and greater than 100 : return true"
else
echo "$a less than 20 and greater than 100 : return false"
fi
Output results:
10 != 20
10 less than 20 or 20 greater than 100 : return true
10 less than 20 and greater than 100 : return false
Logical Operator
operator | Explain | Give an example |
---|---|---|
&& | Logical and | [[$a-lt 100 & & &$b-gt 100]] Returns false |
|| | Logical OR | [[[$a-lt 100 | | b-gt 100]] Returns true |
eg:
#!/bin/bash
a=10
b=20
if [[ $a -lt 100 && $b -gt 100 ]]
then
echo "return true"
else
echo "return false"
fi
if [[ $a -lt 100 || $b -gt 100 ]]
then
echo "return true"
else
echo "return false"
fi
Output results:
return false
return true
String operator
Variable a is "abc" and variable b is "efg".
operator | Explain | Give an example |
---|---|---|
= | Check if two strings are equal and return true equally | [$a = $b], returns false |
!= | Detecting whether two strings are equal and returning true unequally | [$a!= $b], return true |
-z | Detecting whether the length of the string is 0, returning true if it is not 0 | [-z $a] returns false |
-n | Detecting whether the length of the string is 0, returning true if it is not 0 | [-n $a] Returns true |
str | Detect whether the string is empty and return true for none | [$a] Returns true |
eg:
#!/bin/bash
a="abc"
b="efg"
if [ $a = $b ]
then
echo "$a equal $b"
else
echo "$a not equal $b"
fi
if [ $a != $b ]
then
echo "$a != $b"
else
echo "$a == $b"
fi
if [ -n $b ]
then
echo "$b length ${#b}"
else
echo "$b length 0"
fi
# eg: str
if [ $a ]
then
echo "$a not null"
else
echo "$a is null"
fi
Result:
abc not equal efg
abc != efg
abc length 3
efg length 3
abc not null