Shell Scripting – Operators

# Shell Scripting – Operators

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed shell variables.

https://cloudaffaire.com/shell-scripting-shell-variables/

In this blog post, we will discuss shell operators. Like any other programming language, shell also supports operator. Operators are constructs which behave generally like functions, but which differ syntactically or semantically from usual functions. Common simple examples include arithmetic (addition with +), comparison (with >), and logical operations (such as AND or &&) etc. Operator tells interpreter/compiler to perform some mathematical or logical construct on the operands.

### Shell Scripting – Operators:

#### Arithmetic Operators:

Arithmetic operators are used to perform arithmetic operations like the addition of two numbers. Shell only supports arithmetic operations on integers, but you can still perform the floating-point calculation using tools like bc. Below are the arithmetic operators supported by shell –

 Symbol Operation Example + addition 20 + 3 = 23 – subtraction 20 – 3 = 17 * multiplication 20 * 3 = 60 / division 20 / 3 = 6 (by default only returns integer) % modulus 20 % 3 = 2 x++ post-increment 20++ (returns 21 in next run) x– post-decrement 20– (returns 19 in next run) ++x pre-increment ++20 (returns 21 in current run) –x pre-decrement –20 (returns 19 in current run) += increment by constant value 20 +=10 (returns 30) -= decrement by constant value 20 -=10 (returns 10) *= multiplication by constant value 20*=3 (returns 60) /= division by constant value 20/=4 (returns 5) %= mod by constant value 20%=3 (returns 2)

#### Relational Operators:

The relational operator is used to compare two values. Shell supports Equality, Non-equality, Less Than, and Greater Than relational operators. -eq, -nq, -lt, -gt, -le, -ge are only supported for integer comparisons.

 Symbol Operation Example = == -eq Equality 20 -eq 20 returns true, “cat” == “cat” returns true != -nq Non-Equality 20 -nq 10 returns true, “dog” != “cat” returns true > >= -gt -ge Greater Than & Greater Than Equals To “cat” > “dog” returns true, 20 -gt 10 returns true < <= -lt -le Less Than & Less Than Equals To “dog” < “cat” returns true, 10 -lt 20 returns true

#### Boolean Or Logical Operators:

Boolean or Logical operators are used to perform AND, OR, and Not logical operations between two or more operands.

 Symbol Operation Example && -a AND (condition) && (condition) returns true if both conditions are true || -o OR (condition) || (condition) returns true if anyone is true ! NOT !(condition) returns true if condition is false and vice versa

#### Bitwise Operators:

A bitwise operator is an operator used to perform bitwise operations on bit patterns.

 Symbol Operation Example & Bitwise AND Performs AND operation on every bit of operands Example: 00001010 & 00001000 = 00001000 | Bitwise OR Performs OR operation on every bit of operands Example: 00001010 | 00001000 = 00001010 ^ Bitwise XOR Performs XOR operation on every bit of operands Example: 00001010 ^ 00001000 = 00000010 ~ Bitwise NOT Performs NOT operation on every bit of operand Example: ~00001010 = 11110101 >> Bitwise Right Shift Shift bits to right and adds ‘0’s on left Example: >>00001010 = 00000010 << Bitwise Left Shift Shift bits to left and add ‘0’ on left Example: <<00001010 = 00101000

#### File Operators:

File operators are a special set of operators to perform checks on files and directory.

 Symbol Operation Example -e Checks if file exists if [ -e \$file ] returns true if file exist -d Checks if directory exist if [ -d \$directory ] returns true if directory exist -s Checks if file size greater than zero if [ -s \$file ] returns true if file has data -x Checks if file has execute permission if [ -x \$file ] returns true if file has execute permission -w Checks if file has write permission if [ -w \$file ] returns true if file has write permission -r Checks if file has read permission if [ -r \$file ] returns true if file has read permission -b Checks if file type is block device if [ -b \$file ] returns true if file type is block device -c Checks if file type is charecter device if [ -c \$file ] returns true if file type is charecter device -h Checks if file type is symbolic link if [ -h \$file ] returns true if file type is symbolic link -p Checks if file type is pipe if [ -p \$file ] returns true if file type is pipe -S Checks if file type is socket if [ -S \$file ] returns true if file type is socket -O Checks if you are the owner of the file if [ -O \$file ] returns true if you are the owner of the file -G Checks if group-id of the file is same as yours if [ -G \$file ] returns true if you and file owner has same group -N Checks if file has been modified since last read if [ -N \$file ] returns true if file has been modified since last read -k Checks if sticky bit is set on the file if [ -k \$file ] returns true if sticky bit is set on the file -u Checks if sudi is set on the file if [ -u \$file ] returns true if suid is set on the file -g Checks if sgdi is set on the file if [ -g \$file ] returns true if sgid is set on the file

Below are some additional operators that you can use in bash.

Hope you have enjoyed this article. In the next blog post, we will discuss looping in bash.