Open In App

Basic Operators in Shell Scripting

There are 5 basic operators in bash/shell scripting:

1. Arithmetic Operators: These operators are used to perform normal arithmetics/mathematical operations. There are 7 arithmetic operators:




#!/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}
 
mul=$((a*b))
echo "Multiplication of a and b are: "${mul}
 
div=$((a/b))
echo "Division of a and b are: "${div}
 
mod=$((a%b))
echo "Modulis of a and b are: "${mod}
 
((++a))
echo "Increment operator when applied on $a results into a :" "${a}"
 
((--b))
echo "Decrement operator when applied on 'b' results into b :" "${b}"

Output:

2. Relational Operators: Relational operators are those operators which define the relation between two operands. They give either true or false depending upon the relation. They are of 6 types:




#!/bin/bash
  
#reading data from the user
read -p 'Enter a : ' a
read -p 'Enter b : ' b
  
if(( $a==$b ))
then
    echo a is equal to b.
else
    echo a is not equal to b.
fi
  
if(( $a!=$b ))
then
    echo a is not equal to b.
else
    echo a is equal to b.
fi
  
if(( $a<$b ))
then
    echo a is less than b.
else
    echo a is not less than b.
fi
  
if(( $a<=$b ))
then
    echo a is less than or equal to b.
else
    echo a is not less than or equal to b.
fi
  
if(( $a>$b ))
then
    echo a is greater than b.
else
    echo a is not greater than b.
fi
  
if(( $a>=$b ))
then
    echo a is greater than or equal to b.
else
    echo a is not greater than or equal to b.
fi

Output:

3. Logical Operators : They are also known as boolean operators. These are used to perform logical operations. They are of 3 types: 




#!/bin/bash
 
#reading data from the user
read -p 'Enter a : ' a
read -p 'Enter b : ' b
 
if(($a == "true" & $b == "true" ))
then
    echo Both are true.
else
    echo Both are not true.
fi
 
if(($a == "true" || $b == "true" ))
then
    echo Atleast one of them is true.
else
    echo None of them is true.
fi
 
if(( ! $a == "true"  ))
then
    echo "a" was initially false.
else
     echo "a" was initially true.
 fi

Output:

4. Bitwise Operators: A bitwise operator is an operator used to perform bitwise operations on bit patterns. They are of 6 types:




#!/bin/bash
 
#reading data from the user
read -p 'Enter a : ' a
read -p 'Enter b : ' b
 
bitwiseAND=$(( a&b ))
echo Bitwise AND of a and b is $bitwiseAND
 
bitwiseOR=$(( a|b ))
echo Bitwise OR of a and b is $bitwiseOR
 
bitwiseXOR=$(( a^b ))
echo Bitwise XOR of a and b is $bitwiseXOR
 
bitiwiseComplement=$(( ~a ))
echo Bitwise Compliment of a is $bitiwiseComplement
 
leftshift=$(( a<<1 ))
echo Left Shift of a is $leftshift
 
rightshift=$(( b>>1 ))
echo Right Shift of b is $rightshift

Output:

5. File Test Operator: These operators are used to test a particular property of a file.




#!/bin/bash
 
#reading data from the user
read -p 'Enter file name : ' FileName
 
if [ -e $FileName ]
then
    echo File Exist
else
    echo File doesnot exist
fi
 
if [ -s $FileName ]
then
    echo The given file is not empty.
else
    echo The given file is empty.
fi
 
if [ -r $FileName ]
then
    echo The given file has read access.
else
    echo The given file does not has read access.
fi
 
if [ -w $FileName ]
then
    echo The given file has write access.
else
    echo The given file does not has write access.
fi
 
if [ -x $FileName ]
then
    echo The given file has execute access.
else
    echo The given file does not has execute access.
fi

Output:


Article Tags :