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.