Bash -le Operator

The -le operator in Bash is used to check if the first number is “less than or equal to” the second number. It compares two numbers and returns true if the first number is less than or equal to the second.

This operator is part of the comparison operators provided by Bash for performing numeric comparisons.


Syntax of -le Operator

The syntax of -le operator with the two operands is:

</>
Copy
[ num1 -le num2 ]

Here, num1 and num2 are the numbers being compared. The condition returns true if num1 is less than or equal to num2.

If the first number is less than or equal to the second, the condition evaluates to true; otherwise, it evaluates to false.

The -le operator is commonly used in conditional statements like if and while.


Examples of -le (Less Than or Equal To) Operator

Example 1: Checking if a Number is Less Than or Equal to Another

In this example, we take two predefined numbers and check if the first number is less than or equal to the second number using the -le operator.

Program

check_le.sh

</>
Copy
#!/bin/bash
# Define two numbers
num1=5
num2=10

# Compare the numbers
if [ $num1 -le $num2 ]; then
  echo "The first number is less than or equal to the second."
else
  echo "The first number is not less than or equal to the second."
fi

Steps:

  1. Define two numbers: The script initializes two variables, num1 and num2, with values 5 and 10, respectively.
  2. Compare the numbers: The if statement uses the -le operator to check if the value of num1 is less than or equal to num2.
  3. Execute the true condition: If the condition evaluates to true (the first number is less than or equal), the script executes the echo command to display the message "The first number is less than or equal to the second.".
  4. Execute the false condition: If the condition evaluates to false (the first number is not less than or equal), the else block executes, and the script displays the message "The first number is not less than or equal to the second.".

Output

The first number is less than or equal to the second.

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 or equal to the second.

Program

check_le.sh

</>
Copy
#!/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 -le $num2 ]; then
  echo "The first number is less than or equal to the second."
else
  echo "The first number is not less than or equal to the second."
fi

Steps:

  1. Prompt the user to enter the first number: The script displays the message "Enter the first number:" using the echo command and reads the input into the variable num1 using the read command.
  2. 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 variable num2.
  3. Once we have the two numbers, we compare them using the -le operator to check if the first number is less than or equal to the second.

Output

Enter the first number:
5
Enter the second number:
15
The first number is less than or equal to the second.