Unix bash string comparison and numeric comparison - unix1998/technical_notes GitHub Wiki
In bash scripting, gt
and lt
are used for numeric comparisons, while >
and <
are used for string comparisons or file redirection. They are not the same and are used in different contexts:
Numeric Comparisons
-gt
: Greater than-lt
: Less than
These are used within double square brackets [ ... ](/unix1998/technical_notes/wiki/-...-)
or single square brackets [ ... ]
for comparing integers.
Example:
#!/bin/bash
a=5
b=10
if [ $a -gt $b ]; then
echo "$a is greater than $b"
else
echo "$a is not greater than $b"
fi
if [ $a -lt $b ]; then
echo "$a is less than $b"
else
echo "$a is not less than $b"
fi
String Comparisons and Redirection
>
: Greater than (for string comparison) or output redirection<
: Less than (for string comparison) or input redirection
When used for string comparison within double square brackets [ ... ](/unix1998/technical_notes/wiki/-...-)
:
Example:
#!/bin/bash
a="apple"
b="banana"
if [ $a > $b ](/unix1998/technical_notes/wiki/-$a->-$b-); then
echo "$a is greater than $b"
else
echo "$a is not greater than $b"
fi
if [ $a < $b ](/unix1998/technical_notes/wiki/-$a-<-$b-); then
echo "$a is less than $b"
else
echo "$a is not less than $b"
fi
When used for file redirection:
Example:
#!/bin/bash
echo "This will be written to a file" > output.txt
cat < input.txt
Summary
- Use
-gt
and-lt
for numeric comparisons. - Use
>
and<
for string comparisons or file redirection.
They are not interchangeable and should be used appropriately based on the context.
###########################################################################
In bash, the symbols <>
are not used for inequality comparisons. Instead, you can use !=
for both numeric and string comparisons. Here's how you can perform inequality checks for both numeric and string values:
Numeric Comparisons
For numeric inequality, you can use -ne
(not equal to).
Example:
#!/bin/bash
a=5
b=10
if [ $a -ne $b ]; then
echo "$a is not equal to $b"
else
echo "$a is equal to $b"
fi
String Comparisons
For string inequality, you can use !=
.
Example:
#!/bin/bash
a="apple"
b="banana"
if [ "$a" != "$b" ]; then
echo "$a is not equal to $b"
else
echo "$a is equal to $b"
fi
[ ... ](/unix1998/technical_notes/wiki/-...-)
for More Complex Conditions
Using You can also use [ ... ](/unix1998/technical_notes/wiki/-...-)
for more complex string comparisons or conditions involving logical operators.
Example:
#!/bin/bash
a="apple"
b="banana"
if [ "$a" != "$b" ](/unix1998/technical_notes/wiki/-"$a"-!=-"$b"-); then
echo "$a is not equal to $b"
else
echo "$a is equal to $b"
fi
# Example with logical NOT
if [ ! "$a" == "$b" ](/unix1998/technical_notes/wiki/-!-"$a"-==-"$b"-); then
echo "$a is not equal to $b (using logical NOT)"
else
echo "$a is equal to $b (using logical NOT)"
fi
Summary
- For numeric inequality, use
-ne
. - For string inequality, use
!=
. <>
is not used for inequality in bash.- You can use logical NOT (
!
) in combination with[ ... ](/unix1998/technical_notes/wiki/-...-)
for complex conditions.
This approach ensures correct operation in bash scripts for both numeric and string comparisons.