#### Operators

An operator is a symbol that tells to perform specific mathematical or logical manipulations. Shell is rich in built-in operators and provide the following types of operators

This chapter will examine the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

##### Arithmetic Operators

The five arithmetical operations supported

Operator Description
- Subtraction
* Multiplication
/ Division
% Modulo

##### Relational and comparison operators

Two expressions can be compared using relational and equality operators. For example, to know if two values are equal or if one is greater than the other.

The result of such an operation is either true or false (i.e., a Boolean value).

The relational operators are:

operatordescription
==Equal to
!=Not equal to
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to

##### Logical Operators (!, ||, &&)

The operator `!` is operator for the Boolean operation NOT. It has only one operand, to its right, and inverts it, producing false if its operand is true, and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand. For example:

The logical operators `&&` and `||` are used when evaluating two expressions to obtain a single relational result. The operator `&&` corresponds to the Boolean logical operation AND, which yields true if both its operands are true, and false otherwise. The following panel shows the result of operator `&&` evaluating the expression `a && b`:

operator description
&& Called Logical AND operator. If both the operands are non-zero, then condition becomes true.
|| Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true.
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false.
``````!(5 == 5)   // evaluates to false because the expression at its right (5 == 5) is true
!(6 <= 4)   // evaluates to true because (6 <= 4) would be false
!true       // evaluates to false
!false      // evaluates to true``````

##### Conditional ternary operator

The conditional operator evaluates an expression, returning one value if that expression evaluates to true, and a different one if the expression evaluates as false. Its syntax is:

``condition ? result1 : result2``

If condition is true, the entire expression evaluates to result1, and otherwise to result2.

``````7==5 ? 4 : 3     // evaluates to 3, since 7 is not equal to 5.
7==5+2 ? 4 : 3   // evaluates to 4, since 7 is equal to 5+2.
5>3 ? a : b      // evaluates to the value of a, since 5 is greater than 3.
a>b ? a : b      // evaluates to whichever is greater, a or b.``````
##### Bitwise Operators

Bitwise operators modify variables considering the bit patterns that represent the values they store.

Operator Description
& Bitwise AND
| Bitwise inclusive OR
^ Bitwise exclusive OR
~ Unary complement (bit inversion)
<< Shift bits left
>> Shift bits right
##### Precedence of operators

A single expression may have multiple operators. For example:

``x = 5 + 7 % 2``

the above expression always assigns 6 to variable x, because the `%` operator has a higher precedence than the `+` operator, and is always evaluated before. Parts of the expressions can be enclosed in parenthesis to override this precedence order, or to make explicitly clear the intended effect. Notice the difference:

``````x = 5 + (7 % 2)    // x = 6 (same as without parenthesis)
x = (5 + 7) % 2    // x = 0``````

From greatest to smallest priority, Operators are evaluated in the following order:

Level Precedence group Operator Description Grouping
1 Postfix (unary) `++ --` postfix increment / decrement Left-to-right
`()`functional forms
`[]`subscript
`.`member access
2Prefix (unary)`++ --`prefix increment / decrementRight-to-left
`~ !`bitwise NOT / logical NOT
`+ -`unary prefix
4Arithmetic: scaling`* / %`multiply, divide, moduloLeft-to-right
5Arithmetic: addition`+ -`addition, subtractionLeft-to-right
6Bitwise shift`<< >>`shift left, shift rightLeft-to-right
7Relational`< > <= >=`comparison operatorsLeft-to-right
8Equality`== !=`equality / inequalityLeft-to-right
9And`&`bitwise ANDLeft-to-right
10Exclusive or`^`bitwise XORLeft-to-right
11Inclusive or`|`bitwise ORLeft-to-right
12Conjunction`&&`logical ANDLeft-to-right
13Disjunction`||`logical ORLeft-to-right
15Assignment-level expressions`=`assignmentRight-to-left
`?:`conditional operator
16Sequencing`,`comma separatorLeft-to-right

When an expression has two operators with the same precedence level, grouping determines which one is evaluated first: either left-to-right or right-to-left.
Enclosing all sub-statements in parentheses (even those unnecessary because of their precedence) improves code readability.