Bash -lt
Operator
The -lt
operator in Bash is used to check if the first number is “less than” the second number.
This operator is part of the comparison operators provided by Bash for performing numeric comparisons.
Syntax of -lt
Operator
The syntax of -lt
operator with the two operands is:
[ num1 -lt num2 ]
Here, num1
and num2
are the numbers being compared. The condition returns true if num1
is less than num2
.
If the first number is less, the condition evaluates to true; otherwise, it evaluates to false.
The -lt
operator is commonly used in conditional statements like if
and while
.
Examples of -lt
(Less Than) Operator
Example 1: Checking if a Number is Less Than Another
In this example, we take two predefined numbers and check if the first number is less than the second number using the -lt
operator.
Program
check_less.sh
#!/bin/bash
# Define two numbers
num1=10
num2=15
# Compare the numbers
if [ $num1 -lt $num2 ]; then
echo "The first number is less."
else
echo "The first number is not less."
fi
Steps:
- Define two numbers: The script initializes two variables,
num1
andnum2
, with values10
and15
, respectively. - Compare the numbers: The
if
statement uses the-lt
operator to check if the value ofnum1
is less thannum2
. - Execute the true condition: If the condition evaluates to true (the first number is less), the script executes the
echo
command to display the message"The first number is less."
. - Execute the false condition: If the condition evaluates to false (the first number is not less), the
else
block executes, and the script displays the message"The first number is not less."
.
Output
The first number is less.

Example 2: Accepting User Input and Comparing Two Numbers
In this example, we will read two numbers from the user and compare them to check if the first number is less than the second.
Program
check_less_input.sh
#!/bin/bash
# Prompt the user to enter two numbers
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
# Compare the numbers
if [ $num1 -lt $num2 ]; then
echo "The first number is less."
else
echo "The first number is not less."
fi
Steps:
- Prompt the user to enter the first number: The script displays the message
"Enter the first number:"
using theecho
command and reads the input into the variablenum1
using theread
command. - Prompt the user to enter the second number: Similarly, the script prompts the user with the message
"Enter the second number:"
and reads the input into the variablenum2
. - Once we have the two numbers, we compare them using the
-lt
operator to check if the first number is less than the second.
Output
Enter the first number:
5
Enter the second number:
10
The first number is less.

