What is an operator?

Operator is an symbol in programming that performsn an operation on operands

Syntax

operand1 operator operand2

There are two types of operators.

  • Binary Operator: It operates on two operands such as addition, subtraction, multiplication, division, and modulus
  • unary operator: It operats on single operand such as increment and decrement

Bash Arithmetic Operators

Arithmetic operators in Bash provide arithmetic operations such as add, division, subtraction, and division multiplication operators.

OperatorTitleDescriptionExample
+Additionaddition of two or more operandsp+q=50
-Subtractionsubtraction of two or more operandsq-p=10
*Multiplicationmultiplication of two or more operandsp*q=600
/Divideresults quotient after the division of valuesq/p=1.5
%ModulusReturn the remainder after the division of valuesq%p=10
%ModulusReturn the remainder after the division of valuesq%p=10
-exprUnary Minusreverse of an expression-(10-7) is -3
~/Division Intreturns division int value(10~/7) is 1
++IncrementIncrement the value by 1++p=21
--DecrementDecrement the value by 1--q=29

Here is an arithmetic operator example

Assignment Operators

Assignment operators are used to assign the values to an variables. The basic operation is equal(=)

Additionally, THere are other assignment operators.

for example, p is 20

OperationSymbolDescriptionResult
Add Assign+=Addition and assignment to variable((p += 3)) is 23
Substract Assign-=Substract and assignment to variable((p -= 3)) is 17
Multiplicate Assign*=Multiplicatie and assignment to variable((p *= 2)) is 40
Division Assign/=Addition and assignment to variable((p /= 5)) is 4

Bitwise Operators

OperationSymbolDescriptionResult
AND&Bitwise AND of two operands$op1 & $op2 is 0
AND Equal&=Bitwise AND Equal of two operands$op1 & $op2 is 0
OR|Bitwise OR of two operands$op1 | $op2 is 7
XOR^Bitwise XOR of two operands$op1 ^ $op2 is 7
Left Shift<<Bitwise Left Shift of two operands$op1 & $op2 is 0
Left Shift Eql<<=Bitwise Left Shift Equal of two operands$op1| $op2 is 7
XOR^Bitwise XOR of two operands$op1 ^ $op2 is 7
XOR^=Bitwise XOR Equal of two operands$op1 ^ $op2 is 7

Logical operators

These operators are used to perform logicaton operations on variables/expressions/data.

OperationSymbolDescriptionResult
Logical AND&&Return true(exit status=0) if both operands are true, else return false(exit status is non zero)$op1 &&& $op2 is 0
Logical OR||Logical OR of two operands$op1 & $op2 is 0
Logical NOT\!Reverse the conditional value.$op1 s 7

Here is an example

String Comparision Operator

OperationDescription
-z StringReturn true if string is empty, else false.
-n StringREturn true, If string is not empty
str1=str2return true, if str1 and str2 are equal
str1!=str2return true, if str1 and str2 are not equal
str1>str2return true, if str1 sorts before str2
str1<str2return true, if str1 sorts after str2

Numerical Comparision Operator

Following are Comparison operator.

used the -eq operator in the if fi conditional statement

first=13
second=15

if [ "$first" -eq "$second" ]; then
  echo "Two numbers are equal";
fi

| Operation | Name | Description | | :——– | —————— | | -eq | equal | Check if two variables are equal | | -ne | Not equal | Check if two variables are not equal | | -lt | Less than | Check if first variable is less than second variable | | -le | Less than equal | Check first variable is less than equal to second variable | | -gt | greater than | Check if frst variable is greater than second variable | | -ge | greater than equal | Check if first variable is greater than equal to second variable |

Here is an example

p=11
q=5

if [ "$p" -ne "$q" ]; then
  echo "Two numbers are not equal";
fi

Other operators

OperationDescription
-v variableReturns true if an variable set an value, means value is assigned
-o optnameReturns true if an shell optname is enabled
-R variableReturns true if an variable set an value, and it is a named reference