**Arithmetic, Logical. Relational and Bitwise Operations in Bash Scripting**- While writing scripts, you might end up in requiring some arithmetic, logical and relational operations to be performed. Hence, it is of utmost importance to know about these operations. This article will explain you how these operations are done along with the operators used, in bash scripting. This article is pretty straight forward, hence requires less explanation. The key intention is to know various arithmetic, logical and relational operators and their usage in bash scripting.

#### Arithmetic Operations

In bash, we have below arithmetic operators which help us perform corresponding arithmetic operations.

`+`

- Addition`-`

- Subtraction`*`

- Multiplication`/`

- Division`%`

- Modulus (Remainder)

There are three ways with which arithmetic operations can be performed, in a bash script, and these are -

1 .Using

`expr`

**Syntax:**

expr <operand1> <operator> <operand2>

**Example**

# Incorrect way - Don't use quotes here [root@LinuxBox ~]$ expr "10 + 5" 10 + 5 # Correct way - Addition [root@LinuxBox ~]$ expr 10 + 5 15 # Subtraction [root@LinuxBox ~]$ expr 10 - 5 5 # Multiplication - You need to escape '*' with a '\' [root@LinuxBox ~]$ expr 10 * 5 expr: syntax error [root@LinuxBox ~]$ expr 10 \* 5 50 # Division - Again, escape '/' with a '\' [root@LinuxBox ~]$ expr 10 \\ 5 expr: syntax error [root@LinuxBox ~]$ expr 10 \/ 5 2 # Modulus [root@LinuxBox ~]$ expr 10 % 3 1

2. Using double brackets

**Syntax:**

$(( <operand1> <operator> <operand2> ))

**Example:**

# Addition [root@LinuxBox ~]$ echo $((10 + 5)) 15 # Subtraction [root@LinuxBox ~]$ echo $((10 - 5)) 5 # Multiplication [root@LinuxBox ~]$ echo $((10 * 5)) 50 # Division [root@LinuxBox ~]$ echo $((10 / 5)) 2 # Modulus [root@LinuxBox ~]$ echo $((10 % 3)) 1

3. Using

`let`

command**Syntax:**

```
let <expression>
```

**Example:**

# Addition [root@LinuxBox ~]$ let "result = 10 + 5" [root@LinuxBox ~]$ echo $result 15 # Subtraction [root@LinuxBox ~]$ let "result = 10 - 5" [root@LinuxBox ~]$ echo $result 5 # Multiplication [root@LinuxBox ~]$ let "result = 10 * 5" [root@LinuxBox ~]$ echo $result 50 # Division [root@LinuxBox ~]$ let "result = 10 / 5" [root@LinuxBox ~]$ echo $result 2 # Modulus [root@LinuxBox ~]$ let "result = 10 % 3" [root@LinuxBox ~]$ echo $result 1

#### Logical Operations

We now see how logical operations -

**AND**,**OR**and**NOT**, are performed in bash. Just like other programming languages, a '0' is considered as False and everything else as True. These operators evaluate the result to either True or False, based on the conditions. Take a look at logical operators used in bash.1.

`-a`

or `&&`

- Logical AND Operator. Result is True when both operands evaluate to True, else False.True -a True # True True -a False # False False -a True # False False -a False # False

2.

`-o`

or `||`

- Logical OR Operator. Result is False if both operands evaluate to True, else False.True -a True # True True -a False # True False -a True # True False -a False # False

3.

`!`

- NOT Operator. Result is True when operand evaluates to False and vice-versa.! True # False ! False # True

#### Relational Operators

These operators also work on two operands and return True or False accordingly. Check out the relational operators available in bash.

1.

`==`

or `-eq`

- Equals to. Result is True if two operands are equal, else False.2.

`!=`

or `-ne`

- Not equal. Result is True if two operands are not equal, else False.3.

`<`

or `-lt`

- Less than. Result is True if Operand1 is less than Operand 2, else False.4.

`<=`

or `-le`

- Less or equal. Result is True if Operand1 is less or equal to Operand2.5.

`>`

or `-gt`

- Greater than. Result is True if Operand1 is greater than Operand 2, else False.6.

`>=`

or `-ge`

- Greater or equal. Result is True if Operand1 is greater or equal to Operand2.We can use operator symbols like

`==`

, `<`

, `<`

in double parenthesis and check the result using exit status ($?) of the command, where exit status '0' denotes True, otherwise False. While the operators `-eq`

, `-lt`

can be used in `if`

statements, which we will cover in later article in this series.**Example:**

[root@LinuxBox ~]$ (( 5 == 10 )) [root@LinuxBox ~]$ echo $? 1 [root@LinuxBox ~]$ (( 5 != 10 )) [root@LinuxBox ~]$ echo $? 0 [root@LinuxBox ~]$ (( 5 < 10 )) [root@LinuxBox ~]$ echo $? 0 [root@LinuxBox ~]$ (( 5 <= 10 )) [root@LinuxBox ~]$ echo $? 0 [root@LinuxBox ~]$ (( 5 > 10 )) [root@LinuxBox ~]$ echo $? 1 [root@LinuxBox ~]$ (( 5 >= 10 )) [root@LinuxBox ~]$ echo $? 1

#### Bitwise Operators

Bitwise operators process bits or bit patterns, unlike other operators. Basic bitwise operations are listed as below:

`>>`

or Right Shift - Shift bits to right and adds '0's on left (Divide by 2 operation).`<<`

or Left Shift - Shifts bits to left and add '0' on right (Multiply by 2 operation)`&`

or Bitwise AND - Performs AND operation on every bit and produces result`|`

or Bitwise OR - Performs OR operation on every bit and produces result`^`

or Bitwise XOR - Performs XOR operation on every bit and produces result`~`

or Bitwise NOT - Inverts all the bits

**Example :**

# Right Shift 15 >> 3 Ans : 1 # '1111' >> 3 = '0001' 15 >> 1 Ans : 7 # '1111' >> 1 = '0111' # Left Shift 15 << 3 Ans : 120 # '1111' << 3 = '1111000' 15 << 1 Ans : 30 # '1111' << 1 = '11110' # Bitwise AND 15 & 3 Ans : 3 # '1111' & '0011' = '0011' 15 & 1 Ans : 1 # '1111' & '0001' = '0001' # Bitwise OR 15 | 3 Ans : 15 # '1111' | '0011' = '1111' 15 | 1 Ans : 15 # '1111' | '0001' = '1111' # Bitwise XOR 15 ^ 3 Ans : 12 # '1111' ^ '0011' = '1100' 15 ^ 1 Ans : 14 # '1111' ^ '0001' = '1110' # Bitwise NOT ~15 Ans : -16 # ~ '0000 1111' = '1111 0000' ~3 Ans : -4 # ~ '0000 0011' = '1111 1100' ~1 Ans : -2 # ~ '0000 0001' = '1111 1110'

With this, we close this discussion. In this article, we studied various types of operators used in Bash scripts, including Arithmetic, Logical, Relational and Bitwise. Please share your views in the comment section below and stay tuned. Thank you.

## 0 comments:

## Post a Comment