Basic Operators in Shell Scripting
There are 5 basic operators in bash/shell scripting:
- Arithmetic Operators
- Relational Operators
- Boolean Operators
- Bitwise Operators
- File Test Operators
- Arithmetic Operators : These operators are used to perform normal arithmetics/mathematical operations. There are 7 arithmetic operators:
- Addition (+): Binary operation used to add two operands.
- Subtraction (-): Binary operation used to subtract two operands.
- Multiplication (*) :Binary operation used to multiply two operands.
- Division (/) :Binary operation used to divide two operands.
- Modulus (%) :Binary operation used to find remainder of two operands.
- Increment Operator (++) : Uniary operator used to increase the value of operand by one.
- Decrement Operator (–) : Uniary operator used to decrease the value of a operand by one
#!/bin/bash#reading data from the userread- p'Enter a : 'aread- p'Enter b : 'badd= $((a + b))echoAddition of a and b are $addsub= $((a - b))echoSubtraction of a and b are $submul= $((a * b))echoMultiplication of a and b are $muldiv= $((a / b))echodivision of a and b are $divmod= $((a % b))echoModulus of aand b are $mod((++a))echoIncrementoperator when applied on"a"results into a = $a((--b))echoDecrementoperator when applied on"b"results into b = $bOutput:

- Relational Operators : Relational operators are those operators which defines the relation between two operands. They give either true or false depending upon the relation. They are of 6 types:
- ‘==’ Operator : Double equal to operator compares the two operands. Its returns true is they are equal otherwise returns false.
- ‘!=’ Operator : Not Equal to operator return true if the two operands are not equal otherwise it returns false.
- ‘<' Operator : Less than operator returns true if first operand is lees than second operand otherwse returns false.
- ‘<=' Operator : Less than or equal to operator returns true if first operand is less than or equal to second operand otherwise returns false
- ‘>’ Operator : Greater than operator return true if the first operand is greater than the second operand otherwise return false.
- ‘>=’ Operator : Greater than or equal to operator returns true if first operand is greater than or equal to second operand otherwise returns false
#!/bin/bash#reading data from the userread-p'Enter a : 'aread-p'Enter b : 'bif(( $a==$b ))thenechoa is equal to b.elseechoa is not equal to b.fiif(( $a!=$b ))thenechoa is not equal to b.elseechoa is equal to b.fiif(( $a<$b ))thenechoa islessthan b.elseechoa is notlessthan b.fiif(( $a<=$b ))thenechoa islessthan or equal to b.elseechoa is notlessthan or equal to b.fiif(( $a>$b ))thenechoa is greater than b.elseechoa is not greater than b.fiif(( $a>=$b ))thenechoa is greater than or equal to b.elseechoa is not greater than or equal to b.fiOutput:

- Logical Operators : They are also known as boolean operators. These are used to perform logical operations. They are of 3 types:
- Logical AND (&&) : This is a binary operator, which returns true if both the operands are true otherwise returns false.
- Logical OR (||) : This is a binary operator, which returns true is either of the operand is true or both the operands are true and returns false if none of then is false.
- Not Equal to (!) : This is a uninary operator which returns true if the operand is false and returns false if the operand is true.
#!/bin/bash#reading data from the userread-p'Enter a : 'aread-p'Enter b : 'bif(($a =="true"& $b =="true"))thenechoBoth aretrue.elseechoBoth are nottrue.fiif(($a =="true"|| $b =="true"))thenechoAtleast one of them istrue.elseechoNone of them istrue.fiif(( ! $a =="true"))thenecho"a"was intiallyfalse.elseecho"a"was intiallytrue.fiOutput:

- Bitwise Operators : A bitwise operator is an operator used to perform bitwise operations on bit patterns. They are of 6 types:
- Bitwise And (&) : Bitwise & operator performs binary AND operation bit by bit on the operands.
- Bitwise OR (|) : Bitwise | operator performs binary OR operation bit by bit on the operands.
- Bitwise XOR (^) : Bitwise ^ operator performs binary XOR operation bit by bit on the operands.
- Bitwise compliment (~) : Bitwise ~ operator performs binary NOT operation bit by bit on the operand.
- Left Shift (<<) : This operator shifts the bits of the left operand to left by number of times specified by right operand.
- Right Shift (>>) : This operator shifts the bits of the left operand to right by number of times specified by right operand.
#!/bin/bash#reading data from the userread-p'Enter a : 'aread-p'Enter b : 'bbitwiseAND=$(( a&b; ))echoBitwise AND of a and b is $bitwiseANDbitwiseOR=$(( a|b ))echoBitwise OR of a and b is $bitwiseORbitwiseXOR=$(( a^b ))echoBitwise XOR of a and b is $bitwiseXORbitiwiseComplement=$(( ~a ))echoBitwise Compliment of a is $bitiwiseComplementleftshift=$(( a<<1 ))echoLeft Shift of a is $leftshiftrightshift=$(( b>>1 ))echoRight Shift of b is $rightshiftOutput:

- File Test Operator : These operators are used to test a particular property of a file.
- -b operator : This operator check weather a file is a block special file or not. It returns true, if the file is a block special file otherwise false.
- -c operator : This operator checks weather a file is a character special file or not. It returns true if it is a character special file otherwise false.
- -d operator : This operator checks if the given directory exists or not. If it exits then operators returns true otherwise false.
- -e operator : This operator checks weather the given file exits or not. If it exits this operator returns true otherwise false.
- -r operator : This operator checks weather the given file has read access or not. If it has read access then it returns true otherwise false.
- -w operator : This operator check weather the given file has write access or not. If it has write then it returns true otherwise false.
- -x operator : This operator check weather the given file has execute access or not. If it has execute access then it returns true otherwise false.
- -s operator : This operator checks the size of the given file. If the size of given file is greater than 0 then it returns true otherwise it false.
#!/bin/bash#reading data from the userread -p'Enter file name : 'FileNameif[ -e $FileName ]thenecho File Existelseecho File doesnot existfiif[ -s $FileName ]thenecho The given file is not empty.elseecho The given file is empty.fiif[ -r $FileName ]thenecho The given file has read access.elseecho The given file does not has read access.fiif[ -w $FileName ]thenecho The given file has write access.elseecho The given file does not has write access.fiif[ -x $FileName ]thenecho The given file has execute access.elseecho The given file does not has execute access.fiOutput:



