Numerical Comparison Operators
Numerical comparison operators are used to compare numbers within square brackets.
Operators:
-eq
: Equal to-ne
: Not equal to-lt
: Less than-le
: Less than or equal to-gt
: Greater than-ge
: Greater than or equal to
Example:
#!/bin/bash
if [ $1 -eq 100 ]; then
echo "The number is 100."
elif [ $1 -lt 100 ]; then
echo "The number is less than 100."
else
echo "The number is greater than 100."
fi
Output:
$ ./script.sh 100
The number is 100.
$ ./script.sh 50
The number is less than 100.
$ ./script.sh 150
The number is greater than 100.
Using Square Brackets in Bash
Square brackets ([
and ]
) are essential components in Bash scripting. They are used for a variety of purposes, including conditional expressions, string and numerical comparisons, and file tests. This article provides a comprehensive overview of square brackets in Bash, exploring their uses and functionalities.