English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Shell Basic Operators

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

  • Arithmetic operators

  • " does not need to be escaped with a backslash "\".

  • Boolean operators

  • String operators

  • File test operators

Bash does not support simple mathematical operations natively, but can be implemented through other commands, such as awk and expr, with expr being the most commonly used.

expr is an expression evaluation tool that can be used to perform expression evaluation operations.

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

#!/bin/bash
val=`expr 2 + 2`
echo "The sum of the two numbers is : $val"

Execute the script, and the output is as follows:

The sum of the two numbers is : 4

Two points to note:

  •             There must be spaces between expressions and operators, for example 2+2 is incorrect, and must be written as 2 + 2This is different from most programming languages we are familiar with.

  •             The complete expression must be enclosed in ` ` (backticks), note that this character is not the commonly used single quote, located below the Esc key.

Arithmetic operators

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

                Operators                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, and returns true if they are the same.                [ $a == $b ] returns false.
                !=                Not equal. Used to compare two numbers, and returns true if they are not the same.                [ $a != $b ] returns true.

Note:Conditional expressions must be enclosed in square brackets and must have spaces, for example: [$a==$b] is incorrect, and must be written as [ $a == $b ].

Online Examples

The following are examples of arithmetic operators:

Online Examples

#!/bin/bash
# author: Basic Tutorial
# url:www.oldtoolbag.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 is equal to b"
fi
if [$a != $b]
then
   echo "a is not equal to b"
fi

Execute the script, and the output is as follows:

a + b : 30
a - b : -10
a * b : 200
b / a : 2
b % a : 0
a is not equal to b

Note:

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

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

  • The expr syntax in shell on MAC is:$((表达式))$((expression)))*Here, the "

" does not need to be escaped with a backslash "\".

Relational operators

Relational operators only support numbers, not strings, unless the string value is a number. 10, variable b is 20:

                    Operators                    Description                    Example
                    -eq                    The following table lists commonly used relational operators, assuming variable a is                    [ $a -Check if the two numbers are equal, if equal, return true.
                    -ne                    eq $b ] returns false.                    [ $a -Check if the two numbers are not equal, if not equal, return true.
                    -gt                    ne $b ] returns true.                    [ $a -Check if the number on the left is greater than the number on the right, if so, return true.
                    -lt                    gt $b ] returns false.                    [ $a -Check if the number on the left is less than the number on the right, if so, return true.
                    -ge                    lt $b ] returns true.                    [ $a -Check if the number on the left is greater than or equal to the number on the right, if so, return true.
                    -le                    ge $b ] returns false.                    [ $a -Check if the number on the left is less than or equal to the number on the right, if so, return true.

Online Examples

le $b ] returns true.

#!/bin/bash
# author:Basic Tutorial Website
# url:www.oldtoolbag.com
a=10
b=20
if [ $a -Examples of relational operators are as follows:
then
   le $b: a is less than or equal to b" -eq $b ]
else
   le $b: a is less than or equal to b" -eq $b : a is equal to b"
fi
if [ $a -eq $b: a is not equal to b"
then
   le $b: a is less than or equal to b" -ne $b ]
else
   le $b: a is less than or equal to b" -ne $b: a is not equal to b"
fi
if [ $a -ne $b : a is equal to b"
then
   le $b: a is less than or equal to b" -gt $b ]
else
   le $b: a is less than or equal to b" -gt $b: a is greater than b"
fi
if [ $a -gt $b: a is not greater than b"
then
   le $b: a is less than or equal to b" -lt $b ]
else
   le $b: a is less than or equal to b" -lt $b: a is less than b"
fi
if [ $a -lt $b: a is not less than b"
then
   le $b: a is less than or equal to b" -ge $b ]
else
   le $b: a is less than or equal to b" -ge $b: a is greater than or equal to b"
fi
if [ $a -ge $b: a is less than b"
then
   le $b: a is less than or equal to b" -le $b ]
else
   le $b: a is less than or equal to b" -echo "$a
fi

Execute the script, and the output is as follows:

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 less than b
10 -le 20: a is less than or equal to b

Boolean operators

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

                    Operators                    Description                    Example
                    !                    NOT operator, returns false if the expression is true, otherwise returns true.                    [ ! false ] returns true.
                    -o                    OR operator, returns true if at least one expression is true.                    [ $a -lt 20 -o $b -gt 100 ] returns true.
                    -a                    AND operator, returns true only if both expressions are true.                    [ $a -lt 20 -a $b -gt 100 ] returns false.

Online Examples

Examples of boolean operators are as follows:

#!/bin/bash
# author: Basic Tutorial
# url:www.oldtoolbag.com
a=10
b=20
if [$a != $b]
then
   echo "$a != $b: a is not equal to b"
else
   echo "$a == $b: a equals b"
fi
if [ $a -lt 100 -a $b -gt 15 ]
then
   echo "$a less than 100 and $b greater than 15 : returns true
else
   echo "$a less than 100 and $b greater than 15 : returns false
fi
if [ $a -lt 100 -o $b -gt 100]
then
   echo "$a less than 100 or $b greater than 100: returns true
else
   echo "$a less than 100 or $b greater than 100: returns false
fi
if [ $a -lt 5 -o $b -gt 100]
then
   echo "$a less than 5 or $b greater than 100: returns true
else
   echo "$a less than 5 or $b greater than 100: returns false
fi

Execute the script, and the output is as follows:

10 != 20: a is not equal to b
10 less than 100: and 20: greater than 15 : returns true
10 less than 100: or 20: greater than 100: returns true
10 less than 5 or 20: greater than 100: returns false

Logical operators

The following introduces Shell's logical operators, assuming variable a is 10, variable b is 20:

                    Operators                    Description                    Example
                    &&                    Logical AND                    [[ $a -lt 100 && $b -gt 100 ]] returns false
                    ||                    Logical OR                    [[ $a -lt 100 || $b -gt 100 ]] returns true

Online Examples

Examples of logical operators are as follows:

#!/bin/bash
# author:Basic Tutorial Website
# url:www.oldtoolbag.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

Execute the script, and the output is as follows:

Returns false
Returns true

String operators

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

                    Operators                    Description                    Example
                    =                    Check if two strings are equal, return true if equal.                    [ $a = $b ] returns false.
                    !=                    Check if two strings are not equal, return true if not equal.                    [ $a != $b ] returns true.
                    -z                    Check if the string length is 0, return true if 0.                    [ -z $a ] returns false.
                    -n                    Check if the string length is not 0, return true if not 0.                    [ -n "$a" ] returns true.
                    $                    Check if the string is empty, return true if not empty.                    [ $a ] returns true.

Online Examples

Examples of string operators are as follows:

#!/bin/bash
# author:Basic Tutorial Website
# url:www.oldtoolbag.com
a="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"
fi
if [$a != $b]
then
   echo "$a != $b: a is not equal to b"
else
   echo "$a != $b: a is equal to b"
fi
if [ -The end of the string length check
then
   echo "}}-The string length is 0
else
   echo "}}-The string length is not 0
fi
if [ -"$a"
then
   echo "}}-The string length is not 0
else
   echo "}}-n $a : The string length is 0"
fi
if [ $a ]
then
   echo "$a : The string is not empty"
else
   echo "$a : The string is empty"
fi

Execute the script, and the output is as follows:

abc = efg: a is not equal to b
abc != efg : a is not equal to b
-z abc : The string length is not 0
-n abc : The string length is not 0
abc : The string is not empty

File test operators

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

The property detection description is as follows:

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

Other check characters:

  • -S: Determine if a file is a socket.

  • -L: Check if the file exists and is a symbolic link.

Online Examples

The variable file represents the file /var/www/w3codebox/test.sh, it has a size of 100 bytes, with rwx Permissions. The following code will check the various attributes of the file:
#!/bin/bash
# author:Basic Tutorial Website
# url:www.oldtoolbag.com
file="/var/www/w3codebox/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, and 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