This post discuss about different operators supported by MSDOS and Batch Scripting
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
Batch Unary Operator
Pm
Bash Arithmetic Operators
Arithmetic operators in Bash provide arithmetic operations such as add, division, subtraction, and division multiplication operators.
Operator | Title | Description | Example |
---|---|---|---|
+ | Addition | addition of two or more operands | %p%+%q%=50 |
- | Subtraction | subtraction of two or more operands | %q%-%p%=10 |
* | Multiplication | multiplication of two or more operands | %p%*%q%=600 |
/ | Divide | results quotient after the division of values | %q/%p%=1.5 |
%% | Modulus | Return the remainder after the division of values | %q%%p%=10 |
% | Modulus | Return the remainder after the division of values | %q%%p%=10 |
-expr | Unary Minus | reverse of an expression | -(10-7) is -3 |
~/ | Division Int | returns division int value | (10~/7) is 1 |
++ | Increment | Increment the value by 1 | ++%p%=21 |
-- | Decrement | Decrement the value by 1 | --%q%=29 |
Here is an arithmetic operator example
@echo off
SET /A p = 10
SET /A q = 5
SET /A sum = %p% + %q%
echo %sum%
SET /A substract = %p% - %q%
echo %substract%
SET /A divideResult = %p% / %q%
echo %divideResult%
SET /A multResult = %p% * %q%
echo %multResult%
set /A p+=12
echo %p%
set /A p-=12
echo %p%
Output:
15
5
2
50
22
10
Bash Logical Operators
Operator | Title | Description | Example |
---|---|---|---|
AND | Logical AND | addition of two or more operands | p+q=50 |
Assignment Operators
Bitwise Operators
Operation | Symbol | Description | Result |
---|---|---|---|
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 |