Operators
Operators allow you to perform operations on values and variables in Bash. Bash provides various types of operators for different purposes:
Arithmetic Operators
Assignment Operators
Relational Operators
Logical Operators
Bitwise Operators
Ternary Operator
Arithmetic Operator
Arithmetic operators allow you to perform mathematical operations on integers. They combine integer variables and values to produce a new integer result.
+
Addition
$a + $b
-
Subtraction
$a - $b
*
Multiplication
$a * $b
/
Division
$a / $b
%
Modulus (remainder)
$a % $b
**
Exponentiation (a to the power b)
$a ** $b
Assignment Operator
Assignment operators store values or the result of an expression into a variable.
=
Simple assignment
a=5
+=
Add and assign
a+=2
-=
Subtract and assign
a-=3
*=
Multiply and assign
a*=4
/=
Divide and assign
a/=5
%=
Modulus and assign
a%=6
Simple assignment allows storing values in a variable. The combined assignment operators like += and -= allow modifying variables more concisely. For instance, a+=b is the same as a = a + b.
Relational Operator
Relational operators are used for comparing integers and strings to evaluate conditions.
-eq
Equal to
[ $a -eq $b ]
-ne
Not Equal to
[ $a -ne $b ]
-gt
Greater than
[ $a -gt $b ]
-ge
Greater than or equal to
[ $a -ge $b ]
-lt
Less than
[ $a -lt $b ]
-le
Less than or equal to
[ $a -le $b ]
<
Less than (strings)
[ "$a" < "$b" ]
<=
Less than or equal (strings)
[ "$a" <= "$b" ]
>
Greater than (strings)
[ "$a" > "$b" ]
=>
Greater than or equal (strings)
[ "$a" >= "$b" ]
==
Equal to
[ $a == $b ]
!=
Not equal to
[ $a != $b ]
-eq, -ne, -gt, -ge, -lt, and -le work on integers, while <, <=, >, and >= work on string sorting order. These are commonly used in if statements and loops to control program flow. The equal (==) and not equal (!=) operators are useful for comparing integers.
Logical Operator
Logical operators are used for combining and negating conditional expressions.
!
Negation
! [ $a -eq $b ]
&&
AND
[ $a -eq 5 ] && [ $b -eq 10 ]
||
OR
[ $a -eq 5 ] || [ $b -eq 10 ]
The NOT operator (!) inverts a true condition to false or a false condition to true. The AND operator (&&) evaluates to true if both operands are true, while the OR operator (||) evaluates to true if either operand is true. These allow creating complex conditional logic in scripts.
Bitwise Operator
Bitwise operators manipulate integers at the bit level.
&
Bitwise AND
$a & $b
|
Bitwise OR
$a | $b
~
Bitwise NOT
~$a
^
Bitwise XOR
$a ^ $b
<<
Left shift
$a << 2
>>
Right shift
$a >> 2
Bitwise operators treat integers as binary strings and set, unset, or toggle bits at specific positions. This allows bit masking, toggling, and shifting values for flags and low-level binary operations.
Ternary Operator
The ternary operator allows simple conditional expressions.
? :
Evaluates test condition and returns the "if true" or "if false" result
The ternary operator is structured as condition ? resultIfTrue : resultIfFalse
. It tests the given condition and returns the specified result depending on whether the condition evaluated to true or false. This provides a concise way to assign values based on conditions.
Last updated