Shell Basic Operators

Like other programming languages, Shell supports a variety 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; expr is the most commonly used.

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

For example, adding two numbers (Note the use of backticks`not single quotes'):

Examples

#!/bin/bash

val=`expr 2 + 2`
echo The sum of the two numbers is:$val"

Run Example »

Execute the script, and the output result is as follows:

两数之和为 : 4

Two points to note:

  • There must be spaces between expressions and operators. For example, 2+2 is incorrect; it must be written as 2 + 2. This 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 quote; it is below the Esc key.

Arithmetic Operators

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

Operator Description 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.
% Modulo `expr $b % $a` results in 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 not the same. [ $a != $b ] returns true.

Note:Conditional expressions should be placed between square brackets with spaces, for example:[$a==$b]is wrong; it must be written as[ $a == $b ]。

Examples

An example of arithmetic operators is as follows:

Examples

#!/bin/bash
# author:Example
# url:www.example.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

Execute the script, and 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.
  • On MAC, the expr syntax in shell is:$((expression)), here the '*' in the expression does not need the escape character '\'.

Relational Operators

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

The following table lists the 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 equal. [ $a -eq $b ] returns false.
-ne Checks whether two numbers are not equal; returns true if not equal. [ $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.

Examples

An example of relational operators is as follows:

Examples

#!/bin/bash
# author:Example
# url:www.example.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

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 the commonly used Boolean operators, assuming variable a is 10 and variable b is 20:

Operator Description Example
! Negation 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

An example of Boolean operators is as follows:

Examples

#!/bin/bash
# author:Example
# url:www.example.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 "$ais less than 100 and$bgreater than 15 : returns true
else
   echo "$ais less than 100 and$bgreater than 15 : returns false
fi
if [ $a -lt 100 -o $b -gt 100 ]
then
   echo "$ais less than 100 or$bgreater than 100 : returns true
else
   echo "$ais less than 100 or$bgreater than 100 : returns false
fi
if [ $a -lt 5 -o $b -gt 100 ]
then
   echo "$ais less than 5 or$bgreater than 100 : returns true
else
   echo "$ais less than 5 or$bgreater than 100 : returns false
fi

Execute the script, and the output result 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

Examples

An example of logical operators is as follows:

Examples

#!/bin/bash
# author:Example
# url:www.example.com

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

Execute the script, 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
= Check whether two strings are equal; return true if they are equal. [ $a = $b ] returns false.
!= Check whether two strings are not equal; return true if they are not equal. [ $a != $b ] returns true.
-z Check whether the string length is 0; return true if it is 0. [ -z $a ] returns false.
-n Check whether the string length is not 0; return true if it is not 0. [ -n "$a" ] returns true.
$ Check whether the string is not empty; return true if it is not empty. [ $a ] returns true.

Examples

An example of string operators is as follows:

Examples

#!/bin/bash
# author: Example
# url:www.example.com

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 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

Execute the script, 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 detection descriptions are as follows:

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

Other check operators:

  • -S: Check whether a file is a socket.
  • -L: Check whether the file exists and is a symbolic link.

Examples

The variable file represents a file/var/www/example/test.sh, it has a size of 100 bytes and hasrwxpermissions. The following code will check various attributes of this file:

Examples

#!/bin/bash
# author: Example
# url:www.example.com

file="/var/www/example/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

Execute the script, the output is as follows:

File is readable  
File is writable  
File is executable  
File is a regular file  
File is not a directory  
File is not empty  
File exists

Increment and Decrement Operators

Although Shell itself does not have ++ and -- operators like C, C++, or Java, the same functionality can be implemented in other ways. The following are some common methods:

Using the let command

letThe command allows arithmetic operations on integers.

Examples

#!/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.

Examples

#!/bin/bash

# Initialize variables
num=5

# Increment
num=$((num + 1))

# Decrement
num=$((num - 1))

echo $num

Using the expr command

exprThe command can be used for arithmetic operations, but in modern scripts it is not asletand$(( ))commonly used.

Examples

#!/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.

Examples

#!/bin/bash

# Initialize variables
num=5

# Increment
((num++))

# Decrement
((num--))

echo $num

Examples

The following is a complete example script demonstrating the use of increment and decrement operations:

Examples

#!/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 "Using$(( )) after increment:$num"

num=$((num - 1))
echo "Using$(( )) after decrement:$num"

# Using expr
num=$(expr $num + 1)
echo "Using expr after increment:$num"

num=$(expr $num - 1)
echo "Using expr after decrement:$num"

# Using (( ))
((num++))
echo "Using (( )) after increment:$num"

((num--))
echo "Using (( )) after decrement:$num"

Running this script will output the following:

初始值: 5
自增后: 6
自减后: 5
使用 $(( )) 自增后: 6
使用 $(( )) 自减后: 5
使用 expr 自增后: 6
使用 expr 自减后: 5
使用 (( )) 自增后: 6
使用 (( )) 自减后: 5
Other extensions