You can use the following syntax in Bash to check if a number is within a specific range:
if ((num >= 10 && num <= 20))
then
echo "Number in range"
else
echo "Number not in range"
fi
This particular example checks if the number stored in the variable named num is between 10 and 20 and returns one of the following strings as a result:
- “Number in range”
- “Number not in range”
Note that we use the logic num >= 10 && num <=20 to check if the number is greater than or equal to 10 and less than or equal to 20.
The following example shows how to use this syntax in practice.
Example: How to Check if Number is Within a Range in Bash
Suppose we have a variable named num with a value of 17 in Bash.
We can use the following syntax to check if this value is between 10 and 20:
num=17
if ((num >= 10 && num <= 20))
then
echo "Number in range"
else
echo "Number not in range"
fi
The following screenshot shows how to use this syntax in practice:

This returns “Number in range” since the variable named num does indeed contain a value between 10 and 20.
Suppose instead that our variable named num has a value of 7.
We can use the following syntax to check if this value is between 10 and 20:
num=7
if ((num >= 10 && num <= 20))
then
echo "Number in range"
else
echo "Number not in range"
fi
The following screenshot shows how to use this syntax in practice:

This returns “Number not in range” since the variable named num does not contain a value between 10 and 20.
Note: Feel free to return any text you’d like as output in the if else statement.
Related Tutorials
The following tutorials explain how to perform other common tasks in Bash:
Bash: How to Generate a Sequence of Numbers
Bash: How to Round Numbers to 2 Decimal Places
Bash: How to Count Number of Blank Lines in File
