Updated April 10, 2023

## Introduction to Shell Script Operators

At the start of understanding what shell script operators have let us listen to an analogous story and start building a scene of relevance so that it is even simpler to grasp the understanding of operators in shell scripting. In mathematics, we have quite often come across topics of addition, subtraction, multiplication, and division. These are some of the very basic operations which is done on 2 or more elements. In higher-order, we might be using operators on single operands as well. Thus, extending the same usage and similar concepts we have operators in the shell script. These operators are not only limited to numbers or variables as in cases of mathematics but even extended to strings, characters, and even files.

### Types of Shell Script Operators

These operators are divided into 5 different types and now its time to look at them in detail one by one.

#### 1. Arithmetic operator

At the very first type of operator, we have an arithmetic operator. This is assumed to be an extension of the operators we use in mathematics as well. We are already well aware of these operators, but just for the sake of listing the different operators we have:

**Addition operator (+):**This operator is for the addition of 2 operands.**Subtraction operator (-):**This operator is for the subtraction of 2 operands**Multiplication operator (*):**This operator is for multiplication of 2 operands**The division operator (/):**This operator is for the division of 2 operands. It gives only the quotient.**The modulus operator (%):**This operator is for estimating the remainder when one operand is divided by the other.**Increment Operator (++):**This operator is for incrementing the operand’s value by 1.**Decrement Operator (–):**This operator is for decrementing the operand’s value by 1.

#### 2. Relational operator

As the name suggests these operators work on determining the relation between 2 operands. In the case of the relational operator, the output is either true or false, irrespective of the type of operator we would be using. Some of these relational operators are:

- ‘==’ Operator: This operator evaluates the two operands by equating them and process the output as true if they are equal and false if they are not. These operators may be used for integers as well as strings. For strings, one would use it with [[ for pattern matching.
- ‘!=’ Operator: This operator evaluates the two operands by checking the inequality and process the output as true if they are equal and false if they are not. Again, for this operator, one can use it for integer as well as on strings. Essentially for string, this operator would return true if the strings don’t match.
- ‘<‘ Operator: This operator checks for the value of the first operand’s to be less than the second one and return true if that’s the case and for vice versa, it would return false. This is not so widely used in strings!
- ‘<=’ Operator: This operator checks for the value of the first operand’s to be less than or equal to the second one and return true if that’s the case and for vice versa, it would return false. This is not present for strings.
- ‘>’ Operator: This operator checks for the value of the first operand’s to be greater than the second one and returns true if that’s the case and false for vice versa. Not quite widely used for strings.
- ‘>=’ Operator: This operator checks for the value of the first operand’s to be greater than or equal to the second one and return true if that’s the case and false for vice versa. This option is not available for strings.

#### 3. Logical Operator

This set of operators are for analyzing 2 or more set of conditions and return true based on some conditions for each of them.

- Logical AND operator: Only in case of both the set of conditions be true, this operator will process true, else false.
- Logical OR operator: In any case of either of the conditions in any one of the sides be true, this operator will output out TRUE as well.
- NOT operator: This operator reverses the output coming out of the condition and shows that as the output.

#### 4. Bitwise Operators

These operators are pretty much similar to the logical operators except the difference that these operators work on bit patterns. One essential thing to keep in mind is the << or >> are not for less than or greater than, but something explained below.

Some of the operators, with each being self-explanatory are:

- AND (&)
- OR (|)
- XOR (^)
- Compliment
- Shift left (<<)
- Shift right (>>)

#### 5. File operators

The final piece of the puzzle is the file operator, essentially used for testing the file properties:

- -b: Checks if the file is a block special file or not.
- -c: Checks if the file is a character special file or not.
- -d: Checks if the name of the directory exists or not.
- -e: Checks if the file exists or not
- -r: Checks if the file has “read” access or not.
- -w: Checks if the file has “write” access or not.
- -x: Checks if the file has “execute” access or not.
- -s: Checks for the file size and returns if the size is greater than 0.

### Examples of Shell Script Operators

In this section, we would eventually understand the usage of some of the operators we talked about in the earlier section. Respecting the time, you have spent for this article we would go through 2 examples one for integer another for the string so that one is clear on the usage of operators.

#### Example #1

**Code:**

`echo "In this example we would learn about arithmetic operators!"`

a=$1

b=$2

echo "The numbers entered are: $1 and $2"

echo "Addition of the numbers are: $(($1 + $2))"

echo "Multiplication of the numbers are: $(($1 * $2))"

echo "Subtraction of the numbers are: $(($1 - $2))"

echo "Division of the numbers are: $(($1 / $2))"

echo "$1 divided by $2 leaves remainder as: $(($1 % $2))"

((++a))

echo "First number when incremented by 1 is: $a"

((--b))

echo "Second number when decremented by 1 is: $b"

**Output:**

#### Example #2

**Code:**

`echo "In this example we would learn about one example of relational operators using strings!"`

x=$1

y=$2

if [[ "$x" == "$y" ]];

then

echo "$x is equal to $y"

else

echo "$x and $y: Not equal"

fi

**Output:**

### Conclusion

In conclusion, operators play an important role in determining some clausal conditions and allow only a set of commands to be executed as per case basis. This set of rules when resonated with some other shell script capability brings out wonders and becomes the ultimate tool for solving even the complex problems and with a pinch of less coding! Try using the example 2 with integers. It will be fun!

### Recommended Articles

We hope that this EDUCBA information on “Shell Script Operators” was beneficial to you. You can view EDUCBA’s recommended articles for more information.