# 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.

<table><thead><tr><th width="165">Operator</th><th>Description</th><th>Example</th></tr></thead><tbody><tr><td>+</td><td>Addition</td><td>$a + $b</td></tr><tr><td>-</td><td>Subtraction</td><td>$a - $b</td></tr><tr><td>*</td><td>Multiplication</td><td>$a * $b</td></tr><tr><td>/</td><td>Division</td><td>$a / $b</td></tr><tr><td>%</td><td>Modulus (remainder)</td><td>$a % $b</td></tr><tr><td>**</td><td>Exponentiation (a to the power b)</td><td>$a ** $b</td></tr></tbody></table>

```bash
#!/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.

<table><thead><tr><th width="146">Operator</th><th>Description</th><th>Example</th></tr></thead><tbody><tr><td>=</td><td>Simple assignment</td><td>a=5</td></tr><tr><td>+=</td><td>Add and assign</td><td>a+=2</td></tr><tr><td>-=</td><td>Subtract and assign</td><td>a-=3</td></tr><tr><td>*=</td><td>Multiply and assign</td><td>a*=4</td></tr><tr><td>/=</td><td>Divide and assign</td><td>a/=5</td></tr><tr><td>%=</td><td>Modulus and assign</td><td>a%=6</td></tr></tbody></table>

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.

```bash
#!/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.

<table><thead><tr><th width="190">Operator</th><th>Description</th><th>Example</th></tr></thead><tbody><tr><td>-eq</td><td>Equal to</td><td>[ $a -eq $b ]</td></tr><tr><td>-ne</td><td>Not Equal to</td><td>[ $a -ne $b ]</td></tr><tr><td>-gt</td><td>Greater than</td><td>[ $a -gt $b ]</td></tr><tr><td>-ge</td><td>Greater than or equal to</td><td>[ $a -ge $b ]</td></tr><tr><td>-lt</td><td>Less than</td><td>[ $a -lt $b ]</td></tr><tr><td>-le</td><td>Less than or equal to</td><td>[ $a -le $b ]</td></tr><tr><td>&#x3C;</td><td>Less than (strings)</td><td>[ "$a" &#x3C; "$b" ]</td></tr><tr><td>&#x3C;=</td><td>Less than or equal (strings)</td><td>[ "$a" &#x3C;= "$b" ]</td></tr><tr><td>></td><td>Greater than (strings)</td><td>[ "$a" > "$b" ]</td></tr><tr><td>=></td><td>Greater than or equal (strings)</td><td>[ "$a" >= "$b" ]</td></tr><tr><td>==</td><td>Equal to</td><td>[ $a == $b ]</td></tr><tr><td>!=</td><td>Not equal to</td><td>[ $a != $b ]</td></tr></tbody></table>

-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.

```bash
#!/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.

<table><thead><tr><th width="172">Operator</th><th>Description</th><th>Example</th></tr></thead><tbody><tr><td>!</td><td>Negation</td><td>! [ $a -eq $b ]</td></tr><tr><td>&#x26;&#x26;</td><td>AND</td><td>[ $a -eq 5 ] &#x26;&#x26; [ $b -eq 10 ]</td></tr><tr><td>||</td><td>OR</td><td>[ $a -eq 5 ] || [ $b -eq 10 ]</td></tr></tbody></table>

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.

```bash
#!/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.

| Operator | Description | Example  |
| -------- | ----------- | -------- |
| &        | 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.

```bash
#!/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.

<table><thead><tr><th width="217">Syntax</th><th>Description</th><th data-hidden>Example</th></tr></thead><tbody><tr><td>? :</td><td>Evaluates test condition and returns the "if true" or "if false" result</td><td></td></tr></tbody></table>

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.

```bash
#!/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

```
