Arithmetic Operators UNIX
Arithmetic Operators UNIX
+ Addition - Adds values on either side of the operator `expr $a + $b` will give 30
- Subtraction - Subtracts right hand operand from left hand `expr $a - $b` will give -10
operand
* Multiplication - Multiplies values on either side of the `expr $a * $b` will give 200
operator
/ Division - Divides left hand operand by right hand operand `expr $b / $a` will give 2
% Modulus - Divides left hand operand by right hand operand `expr $b % $a` will give 0
and returns remainder
= Assignment - Assign right operand in left operand a=$b would assign value of b into a
== Equality - Compares two numbers, if both are same then [ $a == $b ] would return false.
returns true.
!= Not Equality - Compares two numbers, if both are different [ $a != $b ] would return true.
then returns true.
It is very important to note here that all the conditional expressions would be put inside square braces with one spaces
around them, for example [ $a == $b ] is correct where as [$a==$b] is incorrect.
All the arithmetical calculations are done using long integers.
#!/bin/sh
EXAMPLE
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
Relational Operators:
Bourne Shell supports following relational operators which are specific to numberic values. These operators would not work
for string values unless their value is numerics.
For example, following operators would work to check a relation between 10 and 20 as well as in between "10" and "20" but
not in between "ten" and "twenty".
Assume variable a holds 10 and variable b holds 20 then:
Operator Description Example
-eq Checks if the value of two operands are equal or not, if yes [ $a -eq $b ] is not true.
then condition becomes true.
-ne Checks if the value of two operands are equal or not, if [ $a -ne $b ] is true.
values are not equal then condition becomes true.
-gt Checks if the value of left operand is greater than the value [ $a -gt $b ] is not true.
of right operand, if yes then condition becomes true.
-lt Checks if the value of left operand is less than the value of [ $a -lt $b ] is true.
right operand, if yes then condition becomes true.
-ge Checks if the value of left operand is greater than or equal [ $a -ge $b ] is not true.
to the value of right operand, if yes then condition becomes
true.
-le Checks if the value of left operand is less than or equal to [ $a -le $b ] is true.
the value of right operand, if yes then condition becomes
true.
It is very important to note here that all the conditional expressions would be put inside square braces with one spaces
around them, for example [ $a <= $b ] is correct where as [$a <= $b] is incorrect.
Boolean Operators:
There are following boolean operators supported by Bourne Shell.
Assume variable a holds 10 and variable b holds 20 then:
Operator Description Example
! This is logical negation. This inverts a true condition into [ ! false ] is true.
false and vice versa.
-o This is logical OR. If one of the operands is true then [ $a -lt 20 -o $b -gt 100 ] is true.
condition would be true.
-a This is logical AND. If both the operands are true then [ $a -lt 20 -a $b -gt 100 ] is false.
condition would be true otherwise it would be false.
EXAMPLE
#!/bin/sh
a=10
b=20
if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi
String Operators:
There are following string operators supported by Bourne Shell.
Assume variable a holds "abc" and variable b holds "efg" then:
Operator Description Example
= Checks if the value of two operands are equal or not, if yes [ $a = $b ] is not true.
then condition becomes true.
-z Checks if the given string operand size is zero. If it is zero [ -z $a ] is not true.
length then it returns true.
str Check if str is not the empty string. If it is empty then it [ $a ] is not false.
returns false.
EXAMPLE
/bin/sh
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 : string length is zero"
else
echo "-z $a : string length is not zero"
fi
if [ -n $a ]
then
echo "-n $a : string length is not zero"
else
echo "-n $a : string length is zero"
fi
if [ $a ]
then
echo "$a : string is not empty"
else
echo "$a : string is empty"
fi
-b file Checks if file is a block special file if yes then condition [ -b $file ] is false.
becomes true.
-c file Checks if file is a character special file if yes then condition [ -b $file ] is false.
becomes true.
-d file Check if file is a directory if yes then condition becomes [ -d $file ] is not true.
true.
-g file Checks if file has its set group ID (SGID) bit set if yes then [ -g $file ] is false.
condition becomes true.
-k file Checks if file has its sticky bit set if yes then condition [ -k $file ] is false.
becomes true.
-p file Checks if file is a named pipe if yes then condition becomes [ -p $file ] is false.
true.
-t file Checks if file descriptor is open and associated with a [ -t $file ] is false.
terminal if yes then condition becomes true.
-u file Checks if file has its set user id (SUID) bit set if yes then [ -u $file ] is false.
condition becomes true.
-r file Checks if file is readable if yes then condition becomes true. [ -r $file ] is true.
-w file Check if file is writable if yes then condition becomes true. [ -w $file ] is true.
-x file Check if file is execute if yes then condition becomes true. [ -x $file ] is true.
-s file Check if file has size greater than 0 if yes then condition [ -s $file ] is true.
becomes true.
-e file Check if file exists. Is true even if file is a directory but [ -e $file ] is true.
exists.
EXAMPLE
#!/bin/sh
file="/var/www/tutorialspoint/unix/test.sh"
if [ -r $file ]
then
echo "File has read access"
else
echo "File does not have read access"
fi
if [ -w $file ]
then
echo "File has write permission"
else
echo "File does not have write permission"
fi
if [ -x $file ]
then
echo "File has execute permission"
else
echo "File does not have execute permission"
fi
if [ -f $file ]
then
echo "File is an ordinary file"
else
echo "This is sepcial file"
fi
if [ -d $file ]
then
echo "File is a directory"
else
echo "This is not a directory"
fi
if [ -s $file ]
then
echo "File size is zero"
else
echo "File size is not zero"
fi
if [ -e $file ]
then
echo "File exists"
else
echo "File does not exist"
fi