Shell Basic Operators
Like other programming languages, Shell supports many kinds of operators, including:
- Arithmetic operators
- Relational operators
- Boolean operators
- String operators
- File test operators
Native bash does not support simple mathematical operations, but they can be implemented through other commands, such as awk and expr, of which expr is the most commonly used.
expr is an expression-evaluation tool; using it you can perform evaluation of expressions.
For example, adding two numbers (Note the use of backticks ` rather than single quotes '):
Example
#!/bin/bash
val
=
`
expr
2
2
`
echo
"The sum of the two numbers is: $val"
Run Example »
Run the script, and the output is as follows:
两数之和为 : 4
Two points to note:
- There must be spaces between the expression and the operators; for example 2+2 is incorrect, it must be written as 2 + 2, which is different from most programming languages we are familiar with.
- The complete expression must be
enclosed; note this character is not the usual single quote, it is below the Esc key.
Arithmetic operators
The following table lists commonly used arithmetic operators, assuming variable a is 10 and variable b is 20:
| operator | Description | Example |
|---|---|---|
| + | Addition | expr $a + $b The result is 30. |
| - | Subtraction | expr $a - $b The result is -10. |
| * | Multiplication | expr $a \* $b The result is 200. |
| / | Division | expr $b / $a The result is 2. |
| % | Remainder | expr $b % $a The result is 0. |
| = | Assignment | a=$b assigns the value of variable b to a. |
| == | Equal. Used to compare two numbers; returns true if they are the same. | [ $a == $b ] returns false. |
| != | Not equal. Used to compare two numbers; returns true if they are different. | [ $a != $b ] returns true. |
**Note:** Conditional expressions must be placed between square brackets with spaces, for example: [$a==$b] is incorrect; it must be written as [ $a == $b ].
Example
Examples of arithmetic operators are as follows:
Example
#!/bin/bash
author:Runoob Tutorial
url:www.runoob.com
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 does not equal b"
fi
Run the script, and the output is as follows:
a + b : 30
a - b : -10
a * b : 200
b / a : 2
b % a : 0
a 不等于 b
Note:
Relational operators
Relational operators only support numbers, not strings, unless the string's value is a number.
The following table lists commonly used relational operators, assuming variable a is 10 and variable b is 20:
| operator | Description | Example |
|---|---|---|
| -eq | Checks whether two numbers are equal; returns true if they are. | [ $a -eq $b ] returns false. |
| -ne | Checks whether two numbers are not equal; returns true if they are not. | [ $a -ne $b ] returns true. |
| -gt | Checks whether the number on the left is greater than the one on the right; if so, returns true. | [ $a -gt $b ] returns false. |
| -lt | Checks whether the number on the left is less than the one on the right; if so, returns true. | [ $a -lt $b ] returns true. |
| -ge | Checks whether the number on the left is greater than or equal to the one on the right; if so, returns true. | [ $a -ge $b ] returns false. |
| -le | Checks whether the number on the left is less than or equal to the one on the right; if so, returns true. | [ $a -le $b ] returns true. |
Example
Examples of relational operators are as follows:
Example
#!/bin/bash
author:Runoob Tutorial
url:www.runoob.com
a
=
10
b
=
20
if
[
$a
-eq
$b
]
then
echo
"$a -eq $b : a equals b"
else
echo
"$a -eq $b: a does not equal b"
fi
if
[
$a
-ne
$b
]
then
echo
"$a -ne $b: a does not equal b"
else
echo
"$a -ne $b : a equals 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 than or equal to b"
else
echo
"$a -ge $b: a is 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 is greater than b"
fi
Run the script, and the output is as follows:
10 -eq 20: a 不等于 b
10 -ne 20: a 不等于 b
10 -gt 20: a 不大于 b
10 -lt 20: a 小于 b
10 -ge 20: a 小于 b
10 -le 20: a 小于或等于 b
Boolean operators
The following table lists commonly used Boolean operators, assuming variable a is 10 and variable b is 20:
| operator | Description | Example |
|---|---|---|
| ! | NOT operation: returns false if the expression is true, otherwise returns true. | [ ! false ] returns true. |
| -o | OR operation: returns true if one of the expressions is true. | [ $a -lt 20 -o $b -gt 100 ] returns true. |
| -a | AND operation: returns true only if both expressions are true. | [ $a -lt 20 -a $b -gt 100 ] returns false. |
Example
Examples of Boolean operators are as follows:
Example
#!/bin/bash
author:Runoob Tutorial
url:www.runoob.com
a
=
10
b
=
20
if
[
$a
!
=
$b
]
then
echo
"$a != $b : a does not equal b"
else
echo
"$a == $b: a equals b"
fi
if
[
$a
-lt
100
-a
$b
-gt
15
]
then
echo
"$a is less than 100 and $b is greater than 15: returns true"
else
echo
"$a is less than 100 and $b is greater than 15: returns false"
fi
if
[
$a
-lt
100
-o
$b
-gt
100
]
then
echo
"$a is less than 100 or $b is greater than 100: returns true"
else
echo
"$a is less than 100 or $b is greater than 100: returns false"
fi
if
[
$a
-lt
5
-o
$b
-gt
100
]
then
echo
"$a is less than 5 or $b is greater than 100: returns true"
else
echo
"$a is less than 5 or $b is greater than 100: returns false"
fi
Run the script, and the output is as follows:
10 != 20 : a 不等于 b
10 小于 100 且 20 大于 15 : 返回 true
10 小于 100 或 20 大于 100 : 返回 true
10 小于 5 或 20 大于 100 : 返回 false
Logical operators
The following introduces Shell's logical operators, assuming variable a is 10 and variable b is 20:
| operator | Description | Example |
|---|---|---|
| && | Logical AND | [[ $a -lt 100 && $b -gt 100 ]] returns false |
| || | Logical OR | [[ $a -lt 100 || $b -gt 100 ]] returns true |
Example
Examples of logical operators are as follows:
Example
#!/bin/bash
author:Runoob Tutorial
url:www.runoob.com
a
=
10
b
=
20
if
[
[
$a
-lt
100
&&
$b
-gt
100
]
]
then
echo
"returns true"
else
echo
"returns false"
fi
if
[
[
$a
-lt
100
||
$b
-gt
100
]
]
then
echo
"returns true"
else
echo
"returns false"
fi
Run the script, and the output is as follows:
返回 false
返回 true
String operators
The following table lists commonly used string operators, assuming variable a is "abc" and variable b is "efg":
| operator | Description | Example |
|---|---|---|
| = | Checks whether two strings are equal; returns true if they are. | [ $a = $b ] returns false. |
| != | Checks whether two strings are not equal; returns true if they are not. | [ $a != $b ] returns true. |
| -z | Checks whether a string's length is 0; returns true if it is 0. | [ -z $a ] returns false. |
| -n | Checks whether a string's length is not 0; returns true if it is not 0. | [ -n "$a" ] returns true. |
| $ | Checks whether a string is not empty; returns true if it is not empty. | [ $a ] returns true. |
Example
Examples of string operators are as follows:
Example
#!/bin/bash
author:Runoob Tutorial
url:www.runoob.com
a
=
"abc"
b
=
"efg"
if
[
$a
=
$b
]
then
echo
"$a = $b : a equals b"
else
echo
"$a = $b: a does not equal b"
fi
if
[
$a
!
=
$b
]
then
echo
"$a != $b : a does not equal b"
else
echo
"$a != $b: a equals b"
fi
if
[
-z
$a
]
then
echo
"-z $a : string length is 0"
else
echo
"-z $a : string length is not 0"
fi
if
[
-n
"$a"
]
then
echo
"-n $a : string length is not 0"
else
echo
"-n $a : string length is 0"
fi
if
[
$a
]
then
echo
"$a : string is not empty"
else
echo
"$a : string is empty"
fi
Run the script, and the output is as follows:
abc = efg: a 不等于 b
abc != efg : a 不等于 b
-z abc : 字符串长度不为 0
-n abc : 字符串长度不为 0
abc : 字符串不为空
File test operators
File test operators are used to check various attributes of Unix files.
The attribute checks are described as follows:
| Operator | Description | Example |
|---|---|---|
| -b file | Checks whether a file is a block device file; if so, returns true. | [ -b $file ] returns false. |
| -c file | Checks whether a file is a character device file; if so, returns true. | [ -c $file ] returns false. |
| -d file | Checks whether a file is a directory; if so, returns true. | [ -d $file ] returns false. |
| -f file | Checks whether a file is a regular file (neither a directory nor a device file); if so, returns true. | [ -f $file ] returns true. |
| -g file | Checks whether the SGID bit is set on a file; if so, returns true. | [ -g $file ] returns false. |
| -k file | Checks whether the sticky bit is set on a file; if so, returns true. | [ -k $file ] returns false. |
| -p file | Checks whether a file is a named pipe; if so, returns true. | [ -p $file ] returns false. |
| -u file | Checks whether the SUID bit is set on a file; if so, returns true. | [ -u $file ] returns false. |
| -r file | Checks whether a file is readable; if so, returns true. | [ -r $file ] returns true. |
| -w file | Checks whether a file is writable; if so, returns true. | [ -w $file ] returns true. |
| -x file | Checks whether a file is executable; if so, returns true. | [ -x $file ] returns true. |
| -s file | Checks whether a file is empty (whether its size is greater than 0); returns true if not empty. | [ -s $file ] returns true. |
| -e file | Checks whether a file (including a directory) exists; if so, returns true. | [ -e $file ] returns true. |
Other check operators:
- -S: determines whether a file is a socket.
- -L: checks whether a file exists and is a symbolic link.
Example
The variable file represents a file /var/www/runoob/test.sh, with a size of 100 bytes and rwx permissions. The following code checks various attributes of this file:
Example
#!/bin/bash
author:Runoob Tutorial
url:www.runoob.com
file
=
"/var/www/runoob/test.sh"
if
[
-r
$file
]
then
echo
"File is readable"
else
echo
"file is not readable"
fi
if
[
-w
$file
]
then
echo
"file is writable"
else
echo
"file is not writable"
fi
if
[
-x
$file
]
then
echo
"File is executable"
else
echo
"file is not executable"
fi
if
[
-f
$file
]
then
echo
"file is a regular file"
else
echo
"file is a 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
Run the script, and the output is as follows:
文件可读
文件可写
文件可执行
文件为普通文件
文件不是个目录
文件不为空
文件存在
Increment and decrement operators
Although Shell itself does not have ++ and -- operators like C, C++, or Java, the same functionality can be achieved in other ways. Here are some common methods:
Using the let command
The let command allows arithmetic operations on integers.
Example
#!/bin/bash
Initialize variables
num
=
5
Increment
let
num++
Decrement
let
num--
echo
$num
Using $(( )) for arithmetic operations
The $(( )) syntax is also a way to perform arithmetic operations.
Example
#!/bin/bash
Initialize variables
num
=
5
Increment
num
=$
(
(
num +
1
)
)
Decrement
num
=$
(
(
num -
1
)
)
echo
$num
Using the expr command
The expr command can be used for arithmetic operations, but in modern scripts it is not as let and $(( )) commonly used.
Example
#!/bin/bash
Initialize variables
num
=
5
Increment
num
=$
(
expr
$num
1
)
Decrement
num
=$
(
expr
$num
1
)
echo
$num
Using (( )) for arithmetic operations
Similar to $(( )), the (( )) syntax can also be used for arithmetic operations.
Example
#!/bin/bash
Initialize variables
num
=
5
Increment
(
(
num++
)
)
Decrement
(
(
num--
)
)
echo
$num
Example
The following is a complete example script demonstrating the use of increment and decrement operations:
Example
#!/bin/bash
Initialize variables
num
=
5
echo
"Initial value: $num"
Increment
let
num++
echo
"After increment: $num"
Decrement
let
num--
echo
"After decrement: $num"
Using $(( ))
num
=$
(
(
num +
1
)
)
echo
"After increment using $(( )): $num"
num
=$
(
(
num -
1
)
)
echo
"After decrement using $(( )): $num"
Using expr
num
=$
(
expr
$num
1
)
echo
"After increment using expr: $num"
num
=$
(
expr
$num
1
)
echo
"After decrement using expr: $num"
Using (( ))
(
(
num++
)
)
echo
"After increment using (( )): $num"
(
(
num--
)
)
echo
"After decrement using (( )): $num"
Running this script produces the following output:
初始值: 5
自增后: 6
自减后: 5
使用 $(( )) 自增后: 6
使用 $(( )) 自减后: 5
使用 expr 自增后: 6
使用 expr 自减后: 5
使用 (( )) 自增后: 6
使用 (( )) 自减后: 5