Bash Script Operators
Tags: beginners, devops, cloudcomputing, bash
Published: 2023-10-22 23:08:17

## INTRODUCTION
A Bash shell script, often known as a shell script or just a shell, is a text file that contains a series of commands written in the Bash (Bourne Again Shell) scripting language.

Operators are special symbols or characters used in Bash scripting to carry out various operations on values and variables; these operators are frequently employed with commands to carry out numerous operations, comparisons, and conditional checks.

This article is for beginners who are interested in learning the definition of bash operators and how to use them with Linux commands.

Note: You should have prior knowledge of Shebang and the importance of including them in the script.

You should also have knowledge of Linux permissions.

## KNOW YOUR BASH OPERATORS
There are different categories of bash operators but for the purpose of this article we will focus on four:

- Arithmetic Operators

- Relational or Comparison Operators

- Boolean or Logical Operators

- FileTest Operators

## Arithmetic Operators
These operators are used to perform mathematical operations. The following is a list of the seven bash arithmetic operators:

For the purpose of the explanation of these operators, two variables would be used:
`num1=20`
`num2=5`

**Addition** `+`: This operator is used to add two operands or concatenate two strings and returns the result of that operation.

```
echo $(( num1 + num2 ))
echo $(( 20 + 5 ))
echo $(( 25 ))
output: 25
```
**Subtraction**`-`: This operator is used to subtract one operand from the other and returns the result of that operation.

```
echo $(( num1 - num2 ))
echo $(( 20 - 5 ))
echo $(( 15 ))
output: 15
```
**Multiplication**`*`: This operator is used to multiply two operands and returns the result of that operation.

```
echo $(( num1 * num2 ))
echo $(( 20 * 5 ))
echo $(( 100 ))
output: 100
```
**Division**`/`: This operator is used to divide two operands and returns only the integer part.

```
echo $(( num1 / num2 ))
echo $(( 20 / 5 ))
echo $(( 4 ))
output: 4
```
**Modulus** `%`: This operator is used to divide two operands and returns only the remainder part.

```
echo $(( num1 % num2 ))
echo $(( 20 % 5 ))
echo $(( 0 ))
output: 0
```
**Increment**`++`: This is a unary operator used to increase the operand by one.
Using the same example, we introduce another variable:
`sum=$num1+$num2`

```
echo $(( ++sum ))
output: 26
```
**Decrement**`--`: This is a unary operator used to decrease the operand by one.
Using the same example, we introduce another variable
`sum=$num1*$num2`

```
echo $(( --sum ))
output: 99
```
**An alternative way to use arithmetic operators is to use the `expr` command. **This command is only used for integer values. This means that this command will not work with float numbers. This method will give the same output as the method above.

```
echo $( expr $num1 + $num2 )
echo $( expr $num1 - $num2 )
echo $( expr $num1 / $num2 )
echo $( expr $num1 \* $num2)
```
This is because * without \ will produce a syntax error.

## Relational Operators
These operators specify the relationship between two operands. Depending on the relationship, they either yield true or false. These operators are:

**Equal to **`(==)` or `(-eq)`: It returns `true` if the two operands are equal otherwise returns `false`.

```
var1=40
var2=40

if (( $var1 == $var2 ))
then
    echo True
else
    echo False
fi
```
`output: True`

Another example:

```
var1='banana'
var2='banana'

if [[ "$var1" -eq "$var2" ]]
then
    echo True
else
    echo False
fi
```
`output: True`

**Not Equal to **`(!=)` or `(-ne)`: It returns `true` if the two operands are not equal, otherwise it returns `false`.

```
var1=40
var2=20

if [[ "$var1" -ne "$var2" ]]
then
    echo True
else
    echo False
fi

Another way to write this is:
if (( "$var1" != "$var2" ))
then
    echo True
else
    echo False
fi
```
`output: True`
**Less than **`(<)` or `(le)`: The operator returns `true` if the first operand is less than the second operand otherwise returns `false`.

```
var1=40
var2=20

if [[ "$var1" -lt "$var2" ]]
then
    echo True
else
    echo False
fi

Another way to write this is:
if (( "$var1" < "$var2" ))
then
    echo True
else
    echo False
fi
```
`output: False`

**Greater than** `(>)` or `(-gt)`: It returns `true` if the first operand is greater than the second operand, otherwise it will return `false`

```
var1=40
var2=20

if [[ "$var1" -gt "$var2" ]]
then
    echo True
else
    echo False
fi

Another way to write this is:
if (( "$var1" > "$var2" ))
then
    echo True
else
    echo False
fi
```
`output: True`

**Less or equal to**`(<=)` or `(-le)`: This operator returns `true` if the first operand is less than or equal to the second operand otherwise returns `false`.

```
var1=40
var2=20

if [[ "$var1" -le "$var2" ]]
then
    echo True
else
    echo False
fi

Another way to write this is:
if (( "$var1" <= "$var2" ))
then
    echo True
else
    echo False
fi
```
`output: False`

**Greater or equal to** `(<=)` or `(-ge)`: This operator returns `true` if the first operand is less than or equal to the second operand otherwise returns `false`.

```
var1=40
var2=20

if [[ "$var1" -ge "$var2" ]]
then
    echo True
else
    echo False
fi

Another way to write this is:
if (( "$var1" >= "$var2" ))
then
    echo True
else
    echo False
fi
```
`output: True`

## Boolean Operators
These operators are used for comparing two values or expressions, if they are `true` then it will return `True` else `False`.

**Logical And**`(&&)`: This operator returns `true` if both the operands are `true` otherwise returns `false`.

```
var=40

if [ "$var" -gt 20 -a "$var" -lt 60 ]
then
    echo true
else
    echo false
fi 

Alternatively, it can be written in the following ways:

if ["$var" -gt 20] && ["$var" -lt 60]
then
    echo true
else
    echo false
fi

or this way;
if [[ "$var" -gt 20 && "$var" -lt 60 ]]
then
    echo true
else
    echo false
fi
```
`output: True`

**Logical Or**`(||)`: This operator returns `true` if either of the operands is true or both the operands are true and returns `false` if none of them is false.

```
var=40

if [ "$var" -gt 60 -o "$var" -lt 50 ]
then
    echo true
else
    echo false
fi 

Alternatively, it can be written in the following ways:

if [ "$var" -gt 60 ] || [ "$var" -lt 50 ]
then
    echo true
else
    echo false
fi

or this way;
if [[ "$var" -gt 60 || "$var" -lt 50 ]]
then
    echo true
else
    echo false
fi
```
`output: True`

**Logical Not**`(!)`: This unary operator returns `true` if the operand is false and returns `false` if the operand is true.

```
var=40

if [ ! "$var" -gt 60 -o "$var" -lt 50 ]
then
    echo true
else
    echo false
fi 

Alternatively, it can be written in the following ways:
if [[ ! "$var" -gt 60 && "$var" -lt 50 ]]
then
    echo true
else
    echo false
fi
```
`False`

## File Operators
In Bash, file operators are used to verify and test files and directories. In order to show whether a certain condition is satisfied, file operators return a Boolean value (true or false).

**-b operator**: This operator determines whether or not a file is a block special file. If the file is a block special file, it returns true; otherwise, it returns false.

**-c operator**: This operator determines whether or not a file is a character special file. If the file is a character special file, it returns true; otherwise, it returns false.

**-d operator**: This operator checks if a directory. If it exists it returns true; otherwise, it returns false.

**-e operator**: This operator checks if a file exists. If it exists it returns true; otherwise, it returns false.

**-f operator**: This operator checks if a path exists and points to a regular file (not a directory or device). If it exists it returns true; otherwise, it returns false.

**-l operator**: This operator checks if the path exists and is a symbolic link. If the path exists then it returns true; otherwise, it returns false.

**-r operator**: This operator checks if the file and directory exist and are readable. If the file or directory is readable then it returns true; otherwise, it returns false.

**-s operator**: This operator checks the size of the given file. If the size of the given file is greater than 0 then it returns true; otherwise, it is false

**-w operator**: This operator checks if the file and directory exist and are writable. If the file or directory is writable then it returns true; otherwise, it returns false.

**-x operator**: This operator checks if the file and directory exist and are executable. If the file or directory is executable then it returns true; otherwise, it returns false.

The syntax to use any of the file operators is:


```
if [ fileoperator file-name ]

var=/etc/ssh/sshd_config
if [ -l $var ]
then
    echo ' '
else
    echo ' '
fi
```

## Conclusion
You can write Bash scripts that make decision, carry out computations, and interact with files and directories by using operators such the arithmetic, comparison, logical, and file operators. These operators are necessary for building flexible, error-handling scripts that can respond to a variety of situations.