Class 19 of 37 • Advance Shell Script \t\t\t\t

Operators in Linux Shell Scripting - Arithmetic, Relational & File Tests

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

1. Arithmetic Operators

Operator

Example

Descriptions

+

$(( a + b ))

Perform Addition

-

$(( a - b ))

Perform Subtraction

*

$(( a * b ))

Perform Multiplication

/

$(( a / b ))

Perform Integer Division

%

$(( a % b ))

Return Remainder (Modulo)

**

$(( a ** b ))

Exponentiation (Power)

Complete Working Arithmetic Script

In Bash, arithmetic expressions can be evaluated cleanly using $(( ... )) or using the legacy expr utility:


#!/bin/bash
# ==========================================================
# Arithmetic Operations in Shell Scripting
# ==========================================================

echo -n "Enter First Number: "
read no1

echo -n "Enter Second Number: "
read no2

# Method 1: Using modern Bash arithmetic expansion $(( ... ))
add=$(( no1 + no2 ))
sub=$(( no1 - no2 ))
mul=$(( no1 * no2 ))
div=$(( no1 / no2 ))
rem=$(( no1 % no2 ))

echo "----------------------------------------"
echo "Addition        ($no1 + $no2) = $add"
echo "Subtraction     ($no1 - $no2) = $sub"
echo "Multiplication  ($no1 * $no2) = $mul"
echo "Division        ($no1 / $no2) = $div"
echo "Remainder       ($no1 % $no2) = $rem"
echo "----------------------------------------"

# Method 2: Floating point division with bc (scale sets decimal precision)
float_div=$(echo "scale=3; $no1 / $no2" | bc)
echo "Precise Decimal Division: $float_div"
						

2. Relational (Integer Comparison) Operators

Relational operators compare two integer values inside test conditions [ $a -op $b ]:

Operator Description Example
-eq Checks if two numbers are Equal. [ "$a" -eq "$b" ]
-ne Checks if two numbers are Not Equal. [ "$a" -ne "$b" ]
-gt Checks if left operand is Greater Than right operand. [ "$a" -gt "$b" ]
-ge Checks if left operand is Greater Than or Equal to right operand. [ "$a" -ge "$b" ]
-lt Checks if left operand is Less Than right operand. [ "$a" -lt "$b" ]
-le Checks if left operand is Less Than or Equal to right operand. [ "$a" -le "$b" ]

3. String Comparison Operators

Operator Description Example
= or == Checks if two strings are identical. [ "$str1" = "$str2" ]
!= Checks if two strings are not equal. [ "$str1" != "$str2" ]
-z True if string length is Zero (empty string). [ -z "$str1" ]
-n True if string length is Non-Zero. [ -n "$str1" ]

Frequently Asked Questions (FAQ)

Bash arithmetic evaluation natively truncates all fractions and operates solely on integers. To obtain fractional decimal results, invoke the bc utility: echo "scale=2; 5 / 2" | bc which outputs 2.50.

= is for string comparison (e.g., [ "$name" = "Veewom" ]), whereas -eq is for numerical integer comparison (e.g., [ "$count" -eq 10 ]). Using = for numbers treats them as alphabetical characters, where "05" != "5".

Next Step in Your Linux Learning Path:

Control execution flow with if, elif, else, and case-menu constructs in Class 20: Conditional Statements.

Continue to Class 20: Conditional Statements → Previous: ← Read Command | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!