Operators
An operator is a symbol that tells to perform specific mathematical or logical manipulations. Shell is rich in builtin 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 

+  Addition 
  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:
operator  description 

==  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 nonzero, then condition becomes true. 
  Called Logical OR Operator. If any of the two operands is nonzero, 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.

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  Lefttoright 
()  functional forms  
[]  subscript  
.  member access  
2  Prefix (unary)  ++   prefix increment / decrement  Righttoleft 
~ !  bitwise NOT / logical NOT  
+   unary prefix  
4  Arithmetic: scaling  * / %  multiply, divide, modulo  Lefttoright 
5  Arithmetic: addition  +   addition, subtraction  Lefttoright 
6  Bitwise shift  << >>  shift left, shift right  Lefttoright 
7  Relational  < > <= >=  comparison operators  Lefttoright 
8  Equality  == !=  equality / inequality  Lefttoright 
9  And  &  bitwise AND  Lefttoright 
10  Exclusive or  ^  bitwise XOR  Lefttoright 
11  Inclusive or    bitwise OR  Lefttoright 
12  Conjunction  &&  logical AND  Lefttoright 
13  Disjunction    logical OR  Lefttoright 
15  Assignmentlevel expressions  =  assignment  Righttoleft 
?:  conditional operator  
16  Sequencing  ,  comma separator  Lefttoright 
When an expression has two operators with the same precedence level, grouping determines which one is evaluated first: either lefttoright or righttoleft.
Enclosing all substatements in parentheses (even those unnecessary because of their precedence) improves code readability.
This page is open source.
Noticed a typo? Or something unclear?
Improve this page on GitHub