This is the tutorial about operators in bash, how to use operator in shell script, basic operators in shell scripting, how to use and operator in shell script, how to use arithmetic operators in shell script, how to use logical operators in shell script, operators in basic programming language with examples.
OPERATORS IN SHELL SCRIPT
A special symbol which is used to perform the various tasks such as arithmetic operations, relational operations, logical operations, file testing operations, comparisons, etc, …
- Bash supports the following operators. They are
- Arithmetic operators
- Relational operators
- Logical operators (Boolean operators)
- Special operators like file and directory types
ARITHMETIC OPERATORS
S.N | OPERATOR | DESCRIPTION |
1. | + | Addition |
2. | – | Subtraction |
3. | * | Multiplication |
4. | / | Division |
EXPRESSION IN SHELLS
- Shell provides two options for performing expressions in shell scripts. They are
- Using expr command – Shell Style -> expr
- Using double braces () – C Style -> $(exp)
NOTE
- It is an important to note that, the operator * does not provide the multiplication in shell expression using expr command. Because in shell, the operator * means wild card characters.
- So, the backward slash followed by * symbol \* is to provide the multiplication expression in shell expression using expr command.
I. EXAMPLE OF ARITHMETIC OPERATIONS USING EXPR COMMAND
1. SOURCE CODE
echo “———————————————-“
echo “\tArithmetic Operations using expr command”
echo “———————————————-“
echo “Enter the number 1: “
read a
echo “Enter the number 2: “
read b
# performing arithmetic operations using expr command
r1=`expr $a + $b`
r2=`expr $a – $b`
r3=`expr $a \* $b`
r4=`expr $a / $b`
# print the results
echo “Add: \t$r1”
echo “Sub: \t$r2”
echo “Mul: \t$r3”
echo “Div: \t$r4”
2. OUTPUT
II. EXAMPLE OF ARITHMETIC OPERATIONS USING C STYLE
1. SOURCE CODE
echo “——————————————“
echo “\tArithmetic Operations using C-Style”
echo “——————————————“
echo “Enter the number 1: “
read a
echo “Enter the number 2: “
read b
# performing arithmetic operations using expr command
r1=$((a+b))
r2=$((a-b))
r3=$((a*b))
r4=$((a/b))
# print the results
echo “Add: \t$r1”
echo “Sub: \t$r2”
echo “Mul: \t$r3”
echo “Div: \t$r4”
2. OUTPUT
RELATIONAL OPERATORS
RELATIONAL OPERATORS FOR NUMBERS [ using $() ]
S.N | OPERATOR | DESCRIPTION |
1. | == | Equal |
2. | != | Not Equal |
3. | < | Lesser than |
4. | <= | Lesser than or Equal to |
5. | > | Greater than |
6. | >= | Greater than or Equal to |
NUMERIC COMPARISON OPERATORS FOR NUMBERS
S.N | OPERATOR | DESCRIPTION |
1. | -eq | Equal |
2. | -ne | Not Equal |
3. | -gt | Greater than |
4. | -ge | Greater than or Equal to |
5. | -lt | Lesser than |
6. | -le | Lesser than or Equal to |
RELATIONAL OPERATORS FOR STRINGS [ using if ]
S.N | OPERATOR | DESCRIPTION |
1. | = | Equal |
2. | != | Not Equal |
3. | \< | Lesser than |
4. | \> | Greater than |
III. EXAMPLE OF RELATIONAL OPERATORS USING C STYLE
1. SOURCE CODE
echo “—————————————“
echo “\tRelational Operators using C-Style”
echo “—————————————“
echo “Enter the number 1: “
read a
echo “Enter the number 2: “
read b
# performing relational operators using C style $()
r1=$((a==b))
r2=$((a!=b))
r3=$((a<b))
r4=$((a<=b))
r5=$((a>b))
r6=$((a>=b))
# print the relational results
echo “Equal \t\t\t\t\t\t: $r1”
echo “Not Equal \t\t\t\t\t: $r2”
echo “Lesser than \t\t\t\t: $r3”
echo “Lesser than or Equal to \t: $r4”
echo “Greater than \t\t\t\t: $r5”
echo “Greater than or Equal to \t: $r6”
2. OUTPUT
IV. EXAMPLE OF RELATIONAL OPERATORS FOR STRINGS
1. SOURCE CODE
echo “————————————–“
echo “\tRelational Operators for Strings”
echo “————————————–“
echo “Enter the name 1: “
read a
echo “Enter the name 2: “
read b
# performing relational operators for strings
if [ $a = $b ]
then
echo “———-Equal Results——————“
echo “Both Strings are Equal”
else
echo “———-Equal Results——————“
echo “Both Strings are NOT Equal”
fi
if [ $a != $b ]
then
echo “———-NOT Equal Results——————“
echo “Both Strings are NOT Equal”
else
echo “———-NOT Equal Results——————“
echo “Both Strings are Equal”
fi
if [ $a \> $b ]
then
echo “———-Greater Results——————“
echo “$a is Greater than $b”
else
echo “———-Greater Results——————“
echo “$b is Greater than $a”
fi
if [ $a \< $b ]
then
echo “———-Lesser Results——————“
echo “$a is Lesser than $b”
else
echo “———-Lesser Results——————“
echo “$b is Lesser than $a”
fi
2. OUTPUT
LOGICAL OPERATORS (BOOLEAN OPERATORS)
S.N | OPERATOR | OPERATORS IN SHELL | DESCRIPTION |
1. | Logical AND | && | Binary operator which returns true if both the operands are true otherwise returns false value |
2. | Logical OR | || | Binary operator which returns true if one of the operand or both is true otherwise returns false value |
3. | Not Equal to | ! | Unary operator returns true if the operand is false and returns true if the operand is true |
V. EXAMPLE OF LOGICAL AND OPERATOR
1. SOURCE CODE
echo “—————————————-“
echo “\tLogical AND Operator”
echo “—————————————-“
echo “Enter the number 1: “
read a
echo “Enter the number 2: “
read b
echo “Enter the number 3: “
read c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo “$a is bigger than $b and $c”
elif [ $b -gt $c ]
then
echo “$b is bigger than $a and $b”
else
echo “$c is bigger than $a and $b”
fi
2. OUTPUT
FILE TYPE OPERATORS
S.N | OPERATOR | DESCRIPTION |
1. | -f | Returns true if it exists and if it is a regular file (.txt, .c. sh, etc,…) |
2. | -d | Returns true if exists and if it is a directory |
3. | -e | Returns true if exists |
4. | -z | Returns true if file is empty (file has zero length) |
5. | -r | Returns true if exists and is readable mode |
6. | -w | Returns true if exists and is writable mode |
7. | -x | Returns true if exists and is executable mode |
VI. EXAMPLE OF FILE TEST OPERATORS
1. SOURCE CODE
echo “—————————————————————–“
echo “\tFile Testing Operator”
echo “—————————————————————–“
echo “Enter the file name : “
read fp
if [ -e $fp ]
then
echo “Object Exists…”
if [ -f $fp ]
then
echo “It is a regular file…”
echo “Contents:”
echo $(cat $fp)
elif [ -d $fp ]
then
echo “It is a directory…”
dpath=`pwd`/$fp
echo $dpath
echo “Contents of Directory: “
for i in $(ls $dpath)
do
echo $i
done
else
echo “It is a special file…”
fi
else
echo “Object does not exists…”
fi
2. OUTPUT
DIRECTORY TYPE OPERATORS
S.N | OPERATOR | DESCRIPTION |
1. | ls (OR) ls . | Shows the list of files and folders in current directory |
2. | ls .. | Shows the list of files and folders in parent directory |
3. | ls / | Shows the list of files and folders in root working directory |
4. | ls -l | Shows the files and folders in long listing format |
5. | ls -s | Shows the size of files and folders in current directory |
VII. LISTING FILES AND FOLDERS IN ROOT DIRECTORY
1. SOURCE CODE
(test.sh)
echo “—————————————————-“
echo “\tFiles and Folders in Root Directory”
echo “—————————————————-“
for i in $(ls /)
do
echo $i
done
2. OUTPUT
VIII. LISTING FILES AND FOLDERS IN PARENT DIRECTORY
1. SOURCE CODE
echo “—————————————————-“
echo “\tFiles and Folders in Parent Directory”
echo “—————————————————-“
for i in $(ls ..)
do
echo $i
done
2. OUTPUT
IX. LISTING FILES AND FOLDERS IN CURRENT DIRECTORY
1. SOURCE CODE
echo “—————————————————-“
echo “\tFiles and Folders in Current Directory”
echo “—————————————————-“
for i in $(ls)
do
echo $i
done
2. OUTPUT
MORE TUTORIALS
-
How to convert Word to Number in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get convert word to number in bash script with examples.This is implemented using for loop, tr command, switch case in shell…
-
How to Count Word Frequency in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get the program about how to count word frequency in bash script. This is done by using for loop,…
-
Bash Script – How to convert Number to Words
FacebookTweetPinLinkedInEmailShares0SharePrint In this tutorial, you will see how to convert numbers to words using bash script. This shell script can be done with the help of switch case…