Shell operator


Shell, like other programming languages, supports a variety of operators, including:

  • arithmetic operators

  • relational operations Symbol

  • Boolean operator

  • String operator

  • File test operator

Native bash does not support simple mathematical operations, but it can be achieved through other commands, such as awk and expr, expr is the most commonly used.

expr is an expression calculation tool that can be used to evaluate expressions.

For example, add two numbers (Note that the backtick ` is used instead of the single quote '):

#!/bin/bash

val=`expr 2 + 2`
echo "两数之和为 : $val"

Run the example»

Execute the script, the output result is as follows:

两数之和为 : 4

Two points to note:

  • There must be a space between the expression and the operator. For example, 2+2 is incorrect and must be written as 2 + 2, which is different from most programming languages ​​​​we are familiar with.

  • The complete expression must be enclosed by ` `. Note that this character is not the commonly used single quotation mark, which is under the Esc key.


Arithmetic operators

The following table lists the commonly used arithmetic operators, assuming that variable a is 10 and variable b is 20:

Operator illustrate Example
+ Addition `expr $a + $b` results in 30.
- Subtraction `expr $a - $b` results in 10.
* Multiplication `expr $a \* $b` results in 200.
/ Division `expr $b / $a` results in 2.
% Take remainder `expr $b % $a` results in 0.
= Assignment a=$b will assign the value of variable b to a.
== equal. Used to compare two numbers and return true if they are the same. [ $a == $b ] returns false.
!= not equal. Used to compare two numbers, returning true if they are not the same. [ $a != $b ] returns true.

Note: The conditional expression must be placed between square brackets and there must be spaces, for example: [$a==$b] is wrong and must be written as [ $a == $b ].

Example

The arithmetic operator example is as follows:

#!/bin/bash
# author:php中文网
# url:www.php.cn

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 等于 b"
fi
if [ $a != $b ]
then
   echo "a 不等于 b"
fi

Execute the script, the output result is as follows:

a + b : 30
a - b : -10
a * b : 200
b / a : 2
b % a : 0
a 不等于 b

Note:

  • The multiplication sign (*) must be preceded by a backslash (\) to perform multiplication;

  • if...then...fi is a conditional statement, which will be explained later.


Relational operators

Relational operators only support numbers, not strings, unless the value of the string is a number.

The following table lists commonly used relational operators, assuming that variable a is 10 and variable b is 20:

Operator illustrate Example
-eq Checks whether two numbers are equal, returns true if equal. [ $a -eq $b ] returns false.
-ne Checks whether two numbers are equal, returns true if not equal. [ $a -ne $b ] returns true.
-gt Checks whether the number on the left is greater than the number on the right, if so, returns true. [ $a -gt $b ] returns false.
-lt Checks whether the number on the left is less than the number on the right, if so, returns true. [ $a -lt $b ] returns true.
-ge Check whether the number on the left is greater than the number on the right, if so, return true. [ $a -ge $b ] returns false.
-le Check whether the number on the left is less than or equal to the number on the right, if so, return true. [ $a -le $b ] returns true.

Example

The relational operator example is as follows:

#!/bin/bash
# author:php中文网
# url:www.php.cn

a=10
b=20

if [ $a -eq $b ]
then
   echo "$a -eq $b : a 等于 b"
else
   echo "$a -eq $b: a 不等于 b"
fi
if [ $a -ne $b ]
then
   echo "$a -ne $b: a 不等于 b"
else
   echo "$a -ne $b : a 等于 b"
fi
if [ $a -gt $b ]
then
   echo "$a -gt $b: a 大于 b"
else
   echo "$a -gt $b: a 不大于 b"
fi
if [ $a -lt $b ]
then
   echo "$a -lt $b: a 小于 b"
else
   echo "$a -lt $b: a 不小于 b"
fi
if [ $a -ge $b ]
then
   echo "$a -ge $b: a 大于或等于 b"
else
   echo "$a -ge $b: a 小于 b"
fi
if [ $a -le $b ]
then
   echo "$a -le $b: a 小于或等于 b"
else
   echo "$a -le $b: a 大于 b"
fi

Execute the script and the output result 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 that variable a is 10 and variable b is 20:

Operator illustrate Example
! Non-operation, returns false if the expression is true, otherwise returns true. [ ! false ] Returns true.
-o Or operation, returns true if one expression 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.

Examples

The examples of Boolean operators are as follows:

#!/bin/bash
# author:php中文网
# url:www.php.cn

a=10
b=20

if [ $a != $b ]
then
   echo "$a != $b : a 不等于 b"
else
   echo "$a != $b: a 等于 b"
fi
if [ $a -lt 100 -a $b -gt 15 ]
then
   echo "$a -lt 100 -a $b -gt 15 : 返回 true"
else
   echo "$a -lt 100 -a $b -gt 15 : 返回 false"
fi
if [ $a -lt 100 -o $b -gt 100 ]
then
   echo "$a -lt 100 -o $b -gt 100 : 返回 true"
else
   echo "$a -lt 100 -o $b -gt 100 : 返回 false"
fi
if [ $a -lt 5 -o $b -gt 100 ]
then
   echo "$a -lt 100 -o $b -gt 100 : 返回 true"
else
   echo "$a -lt 100 -o $b -gt 100 : 返回 false"
fi

Execute the script and the output results are as follows:

10 != 20 : a 不等于 b
10 -lt 100 -a 20 -gt 15 : 返回 true
10 -lt 100 -o 20 -gt 100 : 返回 true
10 -lt 100 -o 20 -gt 100 : 返回 false

Logical operators

The following introduces the logical operators of Shell, assuming that variable a is 10 and variable b is 20:

Operator illustrate Example
&& Logical AND [[ $a -lt 100 && $b -gt 100 ]] returns false
|| Logical OR [[ $a -lt 100 || $b -gt 100 ]] Return true

Example

Logical operator examples are as follows:

#!/bin/bash
# author:php中文网
# url:www.php.cn

a=10
b=20

if [[ $a -lt 100 && $b -gt 100 ]]
then
   echo "返回 true"
else
   echo "返回 false"
fi

if [[ $a -lt 100 || $b -gt 100 ]]
then
   echo "返回 true"
else
   echo "返回 false"
fi

Execute the script, the output result is as follows:

返回 false
返回 true

String operators

The following table lists the commonly used string operators, assuming that variable a is "abc ", variable b is "efg":

Operator illustrate Example
= Checks whether two strings are equal, returns true if equal. [ $a = $b ] returns false.
!= Checks whether two strings are equal, returns true if not equal. [ $a != $b ] returns true.
-z Check whether the string length is 0, and return true if it is 0. [ -z $a ] returns false.
-n Check whether the length of the string is 0, and return true if it is not 0. [ -n $a ] returns true.
str Checks whether the string is empty, returns true if not. [ $a ] returns true.

Example

The string operator example is as follows:

#!/bin/bash
# author:php中文网
# url:www.php.cn

a="abc"
b="efg"

if [ $a = $b ]
then
   echo "$a = $b : a 等于 b"
else
   echo "$a = $b: a 不等于 b"
fi
if [ $a != $b ]
then
   echo "$a != $b : a 不等于 b"
else
   echo "$a != $b: a 等于 b"
fi
if [ -z $a ]
then
   echo "-z $a : 字符串长度为 0"
else
   echo "-z $a : 字符串长度不为 0"
fi
if [ -n $a ]
then
   echo "-n $a : 字符串长度不为 0"
else
   echo "-n $a : 字符串长度为 0"
fi
if [ $a ]
then
   echo "$a : 字符串不为空"
else
   echo "$a : 字符串为空"
fi

Execute the script, the output result is as follows:

abc = efg: a 不等于 b
abc != efg : a 不等于 b
-z abc : 字符串长度不为 0
-n abc : 字符串长度不为 0
abc : 字符串不为空

File test operator

File test operators are used to detect various properties of Unix files.

Attribute detection is described as follows:

Operator illustrate Example
-b file Checks if the file is a block device file and returns true if so. [ -b $file ] returns false.
-c file Checks if the file is a character device file and returns true if so. [ -c $file ] returns false.
-d file Checks if the file is a directory and returns true if so. [ -d $file ] returns false.
-f file Checks if the file is a normal file (neither a directory nor a device file) and returns true if so. [ -f $file ] returns true.
-g file Checks if the file has the SGID bit set and returns true if so. [ -g $file ] returns false.
-k file Check whether the file has the sticky bit set, and if so, return true. [ -k $file ] returns false.
-p file Checks if the file is a named pipe and returns true if so. [ -p $file ] returns false.
-u file Checks if the file has the SUID bit set and returns true if so. [ -u $file ] returns false.
-r file Checks if the file is readable and returns true if so. [ -r $file ] returns true.
-w file Checks if the file is writable and returns true if so. [ -w $file ] returns true.
-x file Checks if the file is executable and returns true if so. [ -x $file ] returns true.
-s file Check whether the file is empty (whether the file size is greater than 0), and return true if it is not empty. [ -s $file ] returns true.
-e file Checks whether a file (including a directory) exists and returns true if so. [ -e $file ] returns true.


Example

The variable file represents the file "/var/www/php/test.sh", its size is 100 Bytes, with rwx permissions. The following code will detect various attributes of the file:


#!/bin/bash
# author:php中文网
# url:www.php.cn

file="/var/www/php/test.sh"
if [ -r $file ]
then
   echo "文件可读"
else
   echo "文件不可读"
fi
if [ -w $file ]
then
   echo "文件可写"
else
   echo "文件不可写"
fi
if [ -x $file ]
then
   echo "文件可执行"
else
   echo "文件不可执行"
fi
if [ -f $file ]
then
   echo "文件为普通文件"
else
   echo "文件为特殊文件"
fi
if [ -d $file ]
then
   echo "文件是个目录"
else
   echo "文件不是个目录"
fi
if [ -s $file ]
then
   echo "文件不为空"
else
   echo "文件为空"
fi
if [ -e $file ]
then
   echo "文件存在"
else
   echo "文件不存在"
fi

Execute the script and the output will be as follows:

文件可读
文件可写
文件可执行
文件为普通文件
文件不是个目录
文件不为空
文件存在