This article will introduce the Number comparison operators in Bash.
You can use the following comparison operators to compare Integers in a conditional statement.
Integer comparison | Description | Example |
---|---|---|
-eq | is equal to | if [ "$a" -eq "$b" ] |
-ne | is not equal to | if [ "$a" -ne "$b" ] |
-gt | is greater than | if [ "$a" -gt "$b" ] |
-ge | is greater than or equal to | if [ "$a" -ge "$b" ] |
-lt | is less than | if [ "$a" -lt "$b" ] |
-le | is less than or equal to | if [ "$a" -le "$b" ] |
< | is less than | (("$a" < "$b")) |
<= | is less than or equal to | (("$a" <= "$b")) |
> | is greater than | (("$a" > "$b")) |
>= | is greater than or equal to | (("$a" >= "$b")) |
1. Example
In this example, It compares Integers using -eq
.
#!/bin/bash
a=10
b=10
if [ "$a" -eq "$b" ]
then
echo "a is equal to b"
fi
Output:
$ bash example.sh
a is equal to b
2. Example
In this example, It compares Integers using <
.
#!/bin/bash
c=10
d=20
if (( "$c" < "$d" ))
then
echo "c is less than d"
fi
Output:
$ bash example.sh
c is less than d
Related Posts
- How to measure elapsed time in Bash
- Ceil, Floor, Round off a devided number in Bash
- Check if a number is Positive or Negative in Bash
- How to Print a Variable in Bash
- Number Comparison Operators in Bash
- Convert between Uppercase and Lowercase in Bash
- Check if a File or Directory exists in Bash
- How to extract substring in Bash
- Bash Case Statement with examples
- How to use Command Line Arguments in Bash
- How to use Arrays in Bash
- How to Compare strings in Bash Shell
- if..else statement in Bash
- Add, Subtract, Multiply, Division with Numbers in Bash
- Bash For, Whlie Loop Examples
- How to Pass and Use command line arguments in Bash