Shell Basic Operators
Shell, like other programming languages, supports various operators, including:
- Arithmetic Operators
- Relational Operators
- Boolean Operators
- String Operators
- File Test Operators
Native bash does not support simple arithmetic operations, but they can be achieved 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 evaluation operations on expressions.
For example, adding two numbers (note the use of backticks):
Example
#!/bin/bash
val=`expr 2 + 2`
echo "Sum of two numbers: $val"
Executing the script will output the following:
Sum of two numbers: 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 in
`
, note that this character is not a common single quote, it is located below the Esc key.
Arithmetic Operators
The table below lists 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. |
% | Modulus | expr $b % $a results in 0. |
= | Assignment | a=$b assigns the value of variable b to a. |
== | Equality. Used to compare two numbers, returns true if they are the same. | [ $a == $b ] returns false. |
!= | Inequality. Used to compare two numbers, returns true if they are not the same. | [ $a != $b ] returns true. |
Note: Conditional expressions must be enclosed in square brackets and have spaces, for example, [$a==$b] is incorrect, it must be written as [ $a == $b ].
Example
Arithmetic operator examples are as follows:
Example
#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org
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
Executing the script will output the following:
a + b : 30
a - b : -10
a * b : 200
b / a : 2
b % a : 0
a is not equal to b
Note:
- A backslash () must be added before the asterisk (*) to perform multiplication;
- if...then...fi is a conditional statement, which will be explained later.
- In MAC, the expr syntax for shell is: $((expression)), where "*" in the expression does not need a escape character "\".
Relational Operators
Relational operators only support numbers, not strings, unless the string's value is a number.
The table below lists commonly used relational operators, assuming variable a is 10 and variable b is 20:
Operator | Description | Example |
---|---|---|
-eq | Checks if two numbers are equal, returns true if they are. | [ $a -eq $b ] returns false. |
-ne | Checks if two numbers are not equal, returns true if they are not. | [ $a -ne $b ] returns true. |
-gt | Checks if the left number is greater than the right, returns true if it is. | [ $a -gt $b ] returns false. |
-lt | Checks if the left number is less than the right, returns true if it is. | [ $a -lt $b ] returns true. |
-ge | Checks if the left number is greater than or equal to the right, returns true if it is. | [ $a -ge $b ] returns false. |
-le | Checks if the left number is less than or equal to the right, returns true if it is. | [ $a -le $b ] returns true. |
Example
Relational operator examples are as follows:
Example
#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org
a=10
b=20
if [ $a -eq $b ]
then
echo "a is equal to b"
fi
if [ $a -ne $b ]
then
echo "a is not equal to b"
fi
if [ $a -gt $b ]
then
echo "a is greater than b"
fi
if [ $a -lt $b ]
then
echo "a is less than b"
fi
if [ $a -ge $b ]
then
echo "a is greater than or equal to b"
fi
if [ $a -le $b ]
then
echo "a is less than or equal to b"
fi
Executing the script will output the following:
a is not equal to b
a is less than b
a is less than or equal to b
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 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
Execution of the script, 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 common 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 any 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. |
Example
Boolean operator example as follows:
Example
#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org
a=10
b=20
if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a == $b: a is equal to 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
Execution of the script, the output is as follows:
10 != 20 : a is not equal to b
10 is less than 100 and 20 is greater than 15 : returns true
10 is less than 100 or 20 is greater than 100 : returns true
10 is less than 5 or 20 is greater than 100 : returns 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
Logical operator example as follows:
Example
#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org
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
then
echo "returns true"
else
echo "returns false"
fi
Executing the script, the output is as follows:
returns false
returns true
String Operators
The following table lists common string operators, assuming variable a
is "abc" and variable b
is "efg":
Operator | Description | Example |
---|---|---|
= | Checks if two strings are equal, returns true if they are. | [ $a = $b ] returns false. |
!= | Checks if two strings are not equal, returns true if they are not. | [ $a != $b ] returns true. |
-z | Checks if the string length is zero, returns true if it is. | [ -z $a ] returns false. |
-n | Checks if the string length is non-zero, returns true if it is. | [ -n "$a" ] returns true. |
$ | Checks if the string is not empty, returns true if it is not. | [ $a ] returns true. |
Example
String operator examples are as follows:
#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org
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 [ -z $a ]
then
echo "-z $a : String length is zero"
else
echo "-z $a : String length is not zero"
fi
if [ -n "$a" ]
then
echo "-n $a : String length is not zero"
else
echo "-n $a : String length is zero"
fi
if [ $a ]
then
echo "$a : String is not empty"
else
echo "$a : String is empty"
fi
Executing the script, the output is as follows:
abc = efg: a is not equal to b
abc != efg : a is not equal to b
-z abc : String length is not zero
-n abc : String length is not zero
abc : String is not empty
File Test Operators
File test operators are used to test various properties of Unix files.
Property checks are described as follows:
Operator | Description | Example |
---|---|---|
-b file | Checks if the file is a block device file, returns true if it is. | [ -b $file ] returns false. |
-c file | Checks if the file is a character device file, returns true if it is. | [ -c $file ] returns false. |
-d file | Checks if the file is a directory, returns true if it is. | [ -d $file ] returns false. |
-f file | Checks if the file is a regular file (neither a directory nor a device file), returns true if it is. | [ -f $file ] returns true. |
-g file | Checks if the file has the SGID bit set, returns true if it is. | [ -g $file ] returns false. |
-k file | Checks if the file has the sticky bit set, returns true if it is. | [ -k $file ] returns false. |
-p file | Checks if the file is a named pipe, returns true if it is. | [ -p $file ] returns false. |
-u file | Checks if the file has the SUID bit set, returns true if it is. | [ -u $file ] returns false. |
-r file | Checks if the file is readable, returns true if it is. | [ -r $file ] returns true. |
-w file | Checks if the file is writable, returns true if it is. | [ -w $file ] returns true. |
-x file | Checks if the file is executable, returns true if it is. | [ -x $file ] returns true. |
-s file | Checks if the file is non-empty (file size greater than 0), returns true if it is. | [ -s $file ] returns true. |
-e file | Checks if the file (or directory) exists, returns true if it does. | [ -e $file ] returns true. |
Additional check:
--S : Checks if the file is a socket. --L: Check if the file exists and is a symbolic link.
Example
The variable file
represents the file /var/www/tutorialpro/test.sh, which is 100 bytes in size and has rwx permissions. The following code checks various attributes of this file:
Example
#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org
file="/var/www/tutorialpro/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
Executing 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