# Operators in Linux Programming

Linux shell programming has the following operators,they compare or evaluate mathematical,logical and relational expression.

## Arithmetic Operators

Operator

Example

Descriptions

+

\$a + \$b

-

\$a - \$b

Perform Subtraction

\*

\$a \* \$b

Perform Multiplication

/

\$a / \$b

Perform Division

%

\$a % \$b

Return Reminder

^

\$a ^ \$b

Perform Power value

## Arithmetic Operator Example

write a program to perform all arithmetic operation

``````
#Create a New File With .sh

\$vi file1.sh

echo "Enter Number 1:"

echo "Enter Number 2:"

\$add = `\$expr \$no1 + \$no2'

\$sub = `\$expr \$no1 - \$no2'

\$mul = `\$expr \$no1 \* \$no2'

\$div = `\$expr \$no1 / \$no2'

echo "Subtraction = \$sub"

echo "Multiplication = \$mul"

echo "Division = \$div"

echo "By Veewom"

# Press ESC Key on Keyboard
``````

## Output

``````

\$vi sh file1.sh

Enter Number 1: 50
Enter Number 2: 50

Subtraction = 0
Multiplication = 2500
Division = 1
By Veewom
``````

## Relational Operators

Operator

Example

Descriptions

-eq

\$a -eq \$b

equal to (==)

-ne

\$a -ne \$b

not equal to (!=)

-lt

\$a -lt \$b

less than (<)

-le

\$a -le \$b

less than or equal to (<=)

-gt

\$a -gt \$b

greater than (>)

-gt

\$a -gt \$b

greater than or equal to (>=)

Operator

Example

Descriptions

!

!\$b

Logical NOT

-a

\$a -a \$b

logical AND

-o

\$a -o \$b

Logical OR

## String Operators

Operator

Example

Descriptions

=

\$str = \$str

To check the value of two operands are equal or not.is yes,then condition will be true.

!=

\$str != \$str

To check the value of two operands are equal or not.if they are not equal,then condition will be true

-Z

-z \$str

to check if the given string operands size is zero.if zero length,then it return true.

-n

-n \$str

to check if the given string operand size is non zero.if it is non-zero length,then it return true.

str

str(\$str)

to check if str is not empty string.if it is an empty string then it returns false.

## Test File & Directory Type

Operator

Descriptions

-s file

Non empty file

-f file

is file exist or nomar file and not a directory

-d file

is directory exist and not a file

-w file

is writable

-r file