Bash Script Operators

An operator is a symbol that specifies an action to perform in an expression, for example, the ‘+’ symbol in the expression ‘1 + 2’ would add the two numbers together to produce a result. This type of operator is known as an arithmetic operator, however, there are also other groups of operators within Bash Script that will be explained below.

Arithmetic Operators

Arithmetic operators allow for mathematical calculations to be carried out within Bash Script.

Operator Description
+ Adds two values together.
Subtracts one value from another.
* Multiplies two values together.
/ Divides one value by another.
% Calculates the remainder after one value is divided by another.

The following examples show each of these operators in action.

A=10
B=20

echo $((A + B))
echo $((B - A))
echo $((A * B))
echo $((B / A))
echo $((B % A))

A few things to note here. Firstly, the arithmetic operation is enclosed in two sets of brackets. Secondly, rather than having a dollar sign before each variable name, there is just one before the two open brackets. The output from this is as follows.

30
10
200
2
0

If the result of an arithmetic operation is needed in a variable, it can be achieved in a similar fashion.

A=10
B=20

c=$((A + B))

echo $C

Assignment Operators

Assignment operators in Bash Script are used to assign values to a variable, the most basic of which is ‘=’. Here, the ‘=’ symbol means ‘gets set to’ rather than ‘equals’, so in the expression ‘x = y’, ‘x’ gets set to ‘y’. The arithmetic operator symbols can be combined with ‘=’ to form the assignment operators.

Operator Description
= Assigns the value of the right side operand to the left side operand.
+= Adds the value from the right side operand to the left side operand and assigns the value to the left side operand.
-= Subtracts the value of the right side operand from the left side operand and assigns the value to the left side operand.
*= Multiplies the value from the right side operand with the left side operand and assigns the value to the left side operand.
/= Divides the left side operand by the value of the right side operand and assigns the value to the left side operand.
%= Takes the resulting modulus of the operands on both sides and assigns it to the left hand operand.

The examples below show these assignment operators in action.

A=10
echo $A

B=2
((B += 5))
echo "$B"

C=10
((C -= 4))
echo "$C"

D=2
((D *= 2))
echo "$D"

E=20
((E /= 5))
echo "$E"

F=5
((F %= 2))
echo "$F"

Here is the result of the above examples.

10
7
6
4
4
1

Relational Operators

Relational operators compare two values and are used, for example, in ‘if’ statements, which are described in the section on Decision Making. The result of the comparison is either True or False.

Operator Description
-eq Evaluates to True if the two values are equal.
-ne Evaluates to True if the two values are not equal.
-gt Evaluates to True if the first value is greater than the second.
-lt Evaluates to True if the first value is less than the second.
-ge Evaluates to True if the first value is greater than or equal to the second.
-le Evaluates to True if the first value is less than or equal to the second.

Logical Operators

Logical operators are used where you want to test more than one condition at the same time, for example, in an ‘if’ statement, which is discussed in the section on Decision Making.

Operator Description
-a Logical AND operator where operands on both sides must be True to evaluate to True.
-o Logical OR operator where only one of the two operands must be True to evaluate to True.
! Logical NOT operator that can be used in conjunction with ‘-a’ or ‘-o’ to reverse the resulting operand. In the case of ‘&&’, if both conditions evaluate to True, then with the use of the ‘!’ operator the result would be False.

File Operators

File operators, as the names suggests, work with files, and are often used in an ‘if’ statement, which is discussed in the section on Decision Making, to check various properties of files, including directories.

Operator Description
-b Check if a file is a block special file.
-c Check if a file is a character special file.
-d Check if a file is a directory.
-f Check if a file is an ordinary file as opposed to a directory or special file.
-g Check if a file has its set group ID (SGID) bit set.
-k Check if a file has its sticky bit set.
-p Check if a file is a named pipe.
-t Check if a file descriptor is open and associated with a terminal.
-u Check if a file has its Set User ID (SUID) bit set.
-r Check if a file is readable.
-w Check if a file is writable.
-x Check if a file is executable.
-s Check if a file has a size greater than 0.
-e Checks if a file, including a directory, exists.