1. Introduction

Sometimes, we need to validate whether a number falls within a specified range. For example, we can check if resource utilization is within a range for smooth system operation.

In this tutorial, we’ll discuss methods like conditional statements, arithmetic expressions, functions, and the bc command to check if a number is within a range using the Linux shell.

Moreover, we’re assuming that the range boundaries represent a valid range and are specified using command-line arguments.

2. Using the -ge and -le Conditional Operators

The conditional operators play an important role in making decisions based on the argument we provide to them. Here, we’ll incorporate conditional operators with an if-else conditional statement.

The primary purpose of conditional statements is to introduce decision-making capabilities in code. Usually, we first check the desired condition with an if construct. If it’s true, the code inside its body is executed. Otherwise, the else statement is executed.

Let’s use this construct to check whether a number falls within a range:

$ cat ifrange.sh
#!/bin/bash

number="$1"
lower_limit="$2"
upper_limit="$3"

if [ "$number" -ge "$lower_limit" ] && [ "$number" -le "$upper_limit" ]; then
    echo "The number is within the range"
else
    echo "The number is outside the range"
fi

This script assigns the command-line arguments $1, $2, and $3 to the variables number, lower_limit, and upper_limit, respectively.

Next, we use the if condition to compare the variable number with lower_limit and upper_limit. If the variable number is greater than or equal to (-ge) the lower_limit and less than or equal to (-le) the upper_limit, we see “The number is within the range”. Otherwise, the else statement prints “The number is outside the range”.

Notably, we can also replace the conditional operators -ge and -le with -gt and -lt in the script. This substitution would be appropriate if we intend to exclude upper_limit and lower_limit from the range.

3. Using Arithmetic Expressions

We can use arithmetic expressions to check for the conditions that involve numerical values. The arithmetic expression can help us determine whether a specific relationship between numbers is true or false:

$ cat arithrange.sh
#!/bin/bash

number="$1"
lower_limit="$2"
upper_limit="$3"

if ((number >= lower_limit && number <= upper_limit)); then
    echo "The number is within the range."
else
    echo "The number is outside the range."
fi

In this case, we compare the variable number with lower_limit and upper_limit using the arithmetic expression ((…)) inside the if-else conditional statement. Although it utilizes conditionals, the syntax within an arithmetic expression is different.

Similarly, if we’re doing an exclusive range check, we may need to slightly modify the arithmetic expression inside the if conditional statement:

if ((number > lower_limit && number < upper_limit));

Thus, the expression checks if the variable number is greater than the lower_limit and less than the upper_limit, indicating that it falls within the specified range.

4. Using a Function

Often, we can use functions to break the large script into smaller manageable pieces to improve the overall readability of the code.

Furthermore, once we’ve written the functions, we can reuse them in different parts of the code to minimize redundancy, thereby effectively reducing the code length.

Let’s write another script that defines a function for checking whether a given number is within a range and then calls that function with some example values:

$ cat funcrange.sh
#!/bin/bash

check_range() {
    local number="$1"
    local lower_limit="$2"
    local upper_limit="$3"

    if [ "$number" -ge "$lower_limit" ] && [ "$number" -le "$upper_limit" ]; then
        echo "$number is within the range [$lower_limit, $upper_limit]"
    else
        echo "$number is outside the range [$lower_limit, $upper_limit]"
    fi
}

number=15
lower_limit=10
upper_limit=20

check_range "$number" "$lower_limit" "$upper_limit"

In this example, we define a function named check_range that takes three parameters: number, lower_limit, and upper_limit. Next, we declare three local variables inside the function.

Then, we call check_range with specific values for variable number, lower_limt, and upper_limit. Lastly, we test the values inside the body of the if-else condition.

Finally, the function prints the corresponding message by evaluating the variable number based on the provided range.

Notably, we can access the defined function by name from the command line if we source this file, or we can simply define the function within the shell session.

5. Using the bc Command

The bc (basic calculator) tool is a command-line calculator that’s particularly useful for performing calculations within shell scripts:

$ cat bcrange.sh
#!/bin/bash

number="$1"
lower_limit="$2"
upper_limit="$3"

output=$(echo "$number >= $lower_limit && $number <= $upper_range" | bc)

if [ "$output" -eq 1 ]; then
    echo "The number is within the range."
else
    echo "The number is outside the range."
fi

Instead of directly using numeric values to represent mathematical expressions, we use the echo command to construct a string. This string includes the values of the variables number, upper_limit, and lower_limit.

Then, we pass the constructed string as input to the bc command using the pipe (|) operator. In turn, the bc command evaluates the mathematical expression. If the expression is true, the bc command returns 1; otherwise, it returns 0.

We store the result inside a new variable named output, which we test within the if-else condition.

6. Conclusion

In this article, we learned several ways to check if a certain number is within a desired range.

First, we used conditional statements for basic decision-making, which is not only efficient but also easy to understand. Next, we used arithmetic expressions that offer a concise approach to handle numeric values. Then, we turned to functions as a good choice for enhancing code readability and reusability.

Finally, we used the bc command, which is suitable for handling complex mathematical expressions.