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
#!/bin/bash
# reading data from the user
read -r -p "Enter a: " a
read -r -p "Enter b: " b
add=$((a+b))
echo "Addition of a and b are: "${add}
sub=$((a-b))
echo "Subtraction of a and b are: "${sub}
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.
#!/bin/bash
# Assigning a value to a variable
greeting="Hello, World!"
# Displaying the value of the variable
echo $greeting
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.
#!/bin/bash
# Assigning two variables
num1=10
num2=20
# Using relational operators in an if-else statement
if [ $num1 -eq $num2 ]; then
echo "num1 is equal to num2"
elif [ $num1 -lt $num2 ]; then
echo "num1 is less than num2"
else
echo "num1 is greater than num2"
fi
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.
#!/bin/bash
# Assign values to variables
a=10
b=20
# Check if both conditions are true
if [[ $a -lt 15 && $b -gt 15 ]]; then
echo "Both conditions are true: a is less than 15 and b is greater than 15."
else
echo "One or both conditions are false."
fi
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.
#!/bin/bash
# Assign two numbers
a=5 # Binary: 0101
b=3 # Binary: 0011
# Bitwise AND
and_result=$((a & b))
echo "a & b = $and_result" # Result: 1 (Binary: 0001)
# Bitwise OR
or_result=$((a | b))
echo "a | b = $or_result" # Result: 7 (Binary: 0111)
# Bitwise XOR
xor_result=$((a ^ b))
echo "a ^ b = $xor_result" # Result: 6 (Binary: 0110)
# Bitwise NOT (Note: ~ inverts all the bits, including the sign bit for integers)
not_result=$((~a))
echo "~a = $not_result" # Result: -6 (Binary: Inverted bits of 5)
# Left Shift
left_shift_result=$((a << 1))
echo "a << 1 = $left_shift_result" # Result: 10 (Binary: 1010)
# Right Shift
right_shift_result=$((a >> 1))
echo "a >> 1 = $right_shift_result" # Result: 2 (Binary: 0010)
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.
#!/bin/bash
# Assign value based on a condition
num=5
result=$(if [ $num -gt 3 ]; then echo "Greater than 3"; else echo "Not greater than 3"; fi)
# Display the result
echo $result
Last updated