Shell Basic Operators

There are various operators supported by shell.We will now discuss the following operators:

  • Arithmetic Operators
  • Relational Operators
  • Boolean Operators
  • String Operators
  • File Test Operators

Bourne shell didn’t originally have any mechanism to perform simple arithmetic operations but it uses external programs, either awk or expr and expr is the most commonly used and an expression calculation tool which can complete the expression evaluation operation.

The following example shows how to add two numbers:

#!/bin/sh

val=`expr 2 + 2`
echo "Total value : $val"

The above script will generte the following result:

Total value : 4

Two points need to be considered while adding

  • There must be spaces between operators and expressions. For example, 2+2 is not correct; it should be written as 2 + 2.
  • The complete expression should be enclosed between ‘ ‘, called the backtick.

Arithmetic Operators

The following table list the common arithmetic operators.Assume variable a holds 10 and variable b holds 20.

OperatorDescriptionExample
+ (Addition)Adds values on either side of the operator`expr $a + $b` will give 30
– (Subtraction)Subtracts right hand operand from left hand operand`expr $a – $b` will give -10
* (Multiplication)Multiplies values on either side of the operator`expr $a \* $b` will give 200
/ (Division)Divides left hand operand by right hand operand`expr $b / $a` will give 2
% (Modulus)Divides left hand operand by right hand operand and returns remainder`expr $b % $a` will give 0
= (Assignment)Assigns right operand in left operanda = $b would assign value of b into a
== (Equality)Compares two numbers, if both are same then returns true.[ $a == $b ] would return false.
!= (Not Equality)Compares two numbers, if both are different then returns true.[ $a != $b ] would return true.

Notice:It is very important to understand that all the conditional expressions should be inside square braces with spaces around them, for example [ $a == $b ] is correct whereas, [$a==$b] is incorrect.

The following is example about arithmetic operators:

#!/bin/bash
# author:ylspirit
# url:www.linuxcommands.site

a=10
b=20

val=`expr $a + $b`
echo "a + b : $val"

val=`expr $a - $b`
echo "a - b : $val"

val=`expr $a \* $b`
echo "a * b : $val"

val=`expr $b / $a`
echo "b / a : $val"

val=`expr $b % $a`
echo "b % a : $val"

if [ $a == $b ]
then
   echo "a is equal to  b"
fi
if [ $a != $b ]
then
   echo "a is not equal to  b"
fi

Execute the script and the result is:

a + b : 30
a - b : -10
a * b : 200
b / a : 2
b % a : 0
a is not equal to b

Notice:

  • The backslash (\) must be added before the multiplication sign (*) to achieve multiplication operation;
  • if … then … fi is a conditional statement, which will be explained later.
  • The expx in MAC : $ ((expression)), where the “*” in the expression does not require the escape symbol “\”.

Relational Operators

Relational operators support only numeric not string values and do not work for string values unless their value is numeric.

The following table list the common relational operators.Assume variable a holds 10 and variable b holds 20.

OperatorDescriptionExample
-eqChecks if the value of two operands are equal or not; if yes, then the condition becomes true.[ $a -eq $b ] is not true.
-neChecks if the value of two operands are equal or not; if values are not equal, then the condition becomes true.[ $a -ne $b ] is true.
-gtChecks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true.[ $a -gt $b ] is not true.
-ltChecks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true.[ $a -lt $b ] is true.
-geChecks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true.[ $a -ge $b ] is not true.
-leChecks if the value of left operand is less than or equal to the value of right operand; if yes, then the condition becomes true.[ $a -le $b ] is true.

The following is example about relational operators:

#!/bin/bash
# author:ylspirit
# url:www.linuxcommands.site

a=10
b=20

if [ $a -eq $b ]
then
   echo "$a -eq $b : a is equal to b"
else
   echo "$a -eq $b: a is not equal to b"
fi
if [ $a -ne $b ]
then
   echo "$a -ne $b: a is not equal to b"
else
   echo "$a -ne $b : a is equal to b"
fi
if [ $a -gt $b ]
then
   echo "$a -gt $b: a is greater than b"
else
   echo "$a -gt $b: a is not greater than b"
fi
if [ $a -lt $b ]
then
   echo "$a -lt $b: a is less than b"
else
   echo "$a -lt $b: a is not less than b"
fi
if [ $a -ge $b ]
then
   echo "$a -ge $b:a is greater than or equal to b"
else
   echo "$a -ge $b: a is smaller than b"
fi
if [ $a -le $b ]
then
   echo "$a -le $b: a is less than or equal to b"
else
   echo "$a -le $b: a is greater than b"
fi

Execute the script and the result is:

10 -eq 20: a is not equal to b
10 -ne 20: a is not equal to b
10 -gt 20: a is not greater than b
10 -lt 20: a is less than b
10 -ge 20: a is less than b
10 -le 20: a is less than or equal to b

Boolean Operators

The following table list the common boolean operators.Assume variable a holds 10 and variable b holds 20.

OperatorDescriptionExample
!This is logical negation. This inverts a true condition into false and vice versa.[ ! false ] is true.
-oThis is logical OR. If one of the operands is true, then the condition becomes true.[ $a -lt 20 -o $b -gt 100 ] is true.
-aThis is logical AND. If both the operands are true, then the condition becomes true otherwise false.[ $a -lt 20 -a $b -gt 100 ] is false.

The following is example about boolean operators:

#!/bin/bash
# author:ylspirit
# url:www.linuxcommands.site

a=10
b=20
if[ $a != $b ]
then
echo "$a != $b : a is not equal b"
else
echo "$a == $b: a is equal to b"
fi
if[ $a -lt 100 -a $b -gt 15 ]
then
echo "$a is less than 100 and $b is greater than 15 : return true"
else
echo "$a is less than 100 and $b is greater than 15 : return false"
fi
if[ $a -lt 100 -o $b -gt 100 ]
then
echo "$a is less than 100 or $b is greater than 100 : return true"
else
echo "$a is less than 100 or  $b is greater than 100 : return false"
fi
if[ $a -lt 5 -o $b -gt 100 ]
then
echo "$a is less than 5 or $b is greater than 100 : return true"
else
echo "$a is less than 5 or $b is greater than 100 : return false"
fi

Execute the script and the result is:

10 != 20 : a is not equal b
10 is less than 100 and 20  is greater than 15 : return true
10 is less than 100 or 20  is greater than 100 : return true
10 is less than 5 or 20  is greater than 100 : return false

Logical Operators

The following is example about logical operators:

OperatorDescriptionExample
&&
This is logical AND[[ $a -lt 100 && $b -gt 100 ]] is false
||This is logical OR.[[ $a -lt 100 || $b -gt 100 ]] is true

The following is example about logical operators:

#!/bin/bash
# author:ylspirit
# url:www.linuxcommands.site

a=10
b=20

if [[ $a -lt 100 && $b -gt 100 ]]
then
   echo "is true"
else
   echo "is false"
fi

if [[ $a -lt 100 || $b -gt 100 ]]
then
   echo "is true"
else
   echo "is false"
fi

Execute the script and the result is:

is false
is true

String Operators

The following table list the common string operators.Assume variable a holds “abc” and variable b holds “efg”.

OperatorDescriptionExample
=Checks if the value of two operands are equal or not; if yes, then the condition becomes true.[ $a = $b ] is not true.
!=Checks if the value of two operands are equal or not; if values are not equal then the condition becomes true.[ $a != $b ] is true.
-zChecks if the given string operand size is zero; if it is zero length, then it returns true.[ -z $a ] is not true.
-nChecks if the given string operand size is non-zero; if it is nonzero length, then it returns true.[ -n $a ] is not false.
strChecks if str is not the empty string; if it is empty, then it returns false.[ $a ] is not false.

The following is example about string operators:

#!/bin/bash
# author:ylspirit
# url:www.linuxcommands.site

a="abc"
b="efg"

if [ $a = $b ]
then
   echo "$a = $b : a is equal to b"
else
   echo "$a = $b: a is not equal to b"
fi
if [ $a != $b ]
then
   echo "$a != $b : a is not equal to b"
else
   echo "$a != $b: a is equal to b"
fi
if [ -z $a ]
then
   echo "-z $a : the string size is non-zero"
else
   echo "-z $a : the string size is zero"
fi
if [ -n "$a" ]
then
   echo "-n $a : the string size is non-zero"
else
   echo "-n $a : the string size is non-zero"
fi
if [ $a ]
then
   echo "$a : the string is not the empty"
else
   echo "$a : the string is the empty"
fi

Execute the script and the result is:

abc = efg: a is not equal to b
abc != efg : a is not equal to b
-z abc : the string size is non-zero
-n abc : the string size is non-zero
abc : the string is not the empty

File Test Operators

We have a few operators that can be used to test various properties associated with a Unix file.

OperatorDescriptionExample
-b fileChecks if file is a block special file; if yes, then the condition becomes true.[ -b $file ] is false.
-c fileChecks if file is a character special file; if yes, then the condition becomes true.[ -c $file ] is false.
-d fileChecks if file is a directory; if yes, then the condition becomes true.[ -d $file ] is not true.
-f fileChecks if file is an ordinary file as opposed to a directory or special file; if yes, then the condition becomes true.[ -f $file ] is true.
-g fileChecks if file has its set group ID (SGID) bit set; if yes, then the condition becomes true.[ -g $file ] is false.
-k fileChecks if file has its sticky bit set; if yes, then the condition becomes true.[ -k $file ] is false.
-p fileChecks if file is a named pipe; if yes, then the condition becomes true.[ -p $file ] is false.
-t fileChecks if file descriptor is open and associated with a terminal; if yes, then the condition becomes true.[ -t $file ] is false.
-u fileChecks if file has its Set User ID (SUID) bit set; if yes, then the condition becomes true.[ -u $file ] is false.
-r fileChecks if file is readable; if yes, then the condition becomes true.[ -r $file ] is true.
-w fileChecks if file is writable; if yes, then the condition becomes true.[ -w $file ] is true.
-x fileChecks if file is executable; if yes, then the condition becomes true.[ -x $file ] is true.
-s fileChecks if file has size greater than 0; if yes, then condition becomes true.[ -s $file ] is true.
-e fileChecks if file exists; is true even if file is a directory but exists.[ -e $file ] is true.

Other check operators:

  • -S: Checks if file is socket。
  • -L: Checks if file exists and a symbolic link.

The variable file represents to /var/www/runoob/test.sh, which is 100 bytes in size and has rwx permissions.The following code will detect various attributes of the file:

#!/bin/bash
# author:ylspirit
# url:www.linuxcommands.site

file="/var/www/linuxcommands/test.sh"
if [ -r $file ]
then
   echo "file is readable"
else
   echo "file is not readable"
fi
if [ -w $file ]
then
   echo "file is writable"
else
   echo "file is not writable"
fi
if [ -x $file ]
then
   echo "file is executable"
else
   echo "file is not executable"
fi
if [ -f $file ]
then
   echo "file is an ordinary file"
else
   echo "file is an special file"
fi
if [ -d $file ]
then
   echo "file is a directory"
else
   echo "file is not a directory"
fi
if [ -s $file ]
then
   echo "file has size greater than 0"
else
   echo "file size is 0"
fi
if [ -e $file ]
then
   echo "file exists"
else
   echo "file does not exist"
fi

Execute the script and the result is:

file is readable
file is writable
file is readable
file is executable
file is not a directory
file has size greater than 0
file exists

Add a Comment

Your email address will not be published. Required fields are marked *