Bash Comparison Operators
Integer comparison
String comparisonInteger comparison
- is equal to if [ "$a" -eq "$b" ]
is not equal to if [ "$a" -ne "$b" ]
is greater than if [ "$a" -gt "$b" ]
is greater than or equal to if [ "$a" -ge "$b" ]
is less than if [ "$a" -lt "$b" ]
is less than or equal to if [ "$a" -le "$b" ]
is less than if (("$a" < "$b"))
is less than or equal to if (("$a" <= "$b"))
is greater than if (("$a" > "$b"))
is greater than or equal to if (("$a" >= "$b"))
- is equal to if [ "$a" = "$b" ]
(Note the whitespace framing the =. if [ "$a"="$b" ] is not equivalent to the above.) - is equal to if [ "$a" == "$b" ]
(This is a synonym for =.)
(The == comparison operator behaves differently within a double-brackets test than within single brackets.)
[[ $a == z* ]] # True if $a starts with an "z" (pattern matching). [[ $a == "z*" ]] # True if $a is equal to z* (literal matching). [ $a == z* ] # File globbing and word splitting take place. [ "$a" == "z*" ] # True if $a is equal to z* (literal matching). # Thanks, Stéphane Chazelas
- is not equal to if [ "$a" != "$b" ]
(This operator uses pattern matching within a [[ ... ]] construct.) - is less than, in ASCII alphabetical order
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ]
(Note that the "<" needs to be escaped within a [ ] construct.) - is greater than, in ASCII alphabetical order
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ]
(Note that the ">" needs to be escaped within a [ ] construct.See Example 27-11 for an application of this comparison operator.) - string is null, that is, has zero length if [ -z "$String" ]
String='' # Zero-length ("null") string variable. if [ -z "$String" ] then echo "\$String is null." else echo "\$String is NOT null." fi # $String is null. - string is not null. if [ -n "$String" ]
The -n test requires that the string be quoted within the test brackets. Using an unquoted string with ! -z, or even just the unquoted string alone within test brackets (see Example 7-6) normally works, however, this is an unsafe practice. Always quote a tested string. [1]
Example 7-5. Arithmetic and string comparisons
#!/bin/bash a=4 b=5 # Here "a" and "b" can be treated either as integers or strings. # There is some blurring between the arithmetic and string comparisons, #+ since Bash variables are not strongly typed. # Bash permits integer operations and comparisons on variables #+ whose value consists of all-integer characters. # Caution advised, however. echo if [ "$a" -ne "$b" ] then echo "$a is not equal to $b" echo "(arithmetic comparison)" fi echo if [ "$a" != "$b" ] then echo "$a is not equal to $b." echo "(string comparison)" # "4" != "5" # ASCII 52 != ASCII 53 fi # In this particular instance, both "-ne" and "!=" work. echo exit 0 |
Example 7-6. Testing whether a string is null
#!/bin/bash
# str-test.sh: Testing null strings and unquoted strings,
#+ but not strings and sealing wax, not to mention cabbages and kings . . .
# Using if [ ... ]
# If a string has not been initialized, it has no defined value.
# This state is called "null" (not the same as zero!).
if [ -n $string1 ] # string1 has not been declared or initialized.
then
echo "String \"string1\" is not null."
else
echo "String \"string1\" is null."
fi # Wrong result.
# Shows $string1 as not null, although it was not initialized.
echo
# Let's try it again.
if [ -n "$string1" ] # This time, $string1 is quoted.
then
echo "String \"string1\" is not null."
else
echo "String \"string1\" is null."
fi # Quote strings within test brackets!
echo
if [ $string1 ] # This time, $string1 stands naked.
then
echo "String \"string1\" is not null."
else
echo "String \"string1\" is null."
fi # This works fine.
# The [ ... ] test operator alone detects whether the string is null.
# However it is good practice to quote it (if [ "$string1" ]).
#
# As Stephane Chazelas points out,
# if [ $string1 ] has one argument, "]"
# if [ "$string1" ] has two arguments, the empty "$string1" and "]"
echo
string1=initialized
if [ $string1 ] # Again, $string1 stands unquoted.
then
echo "String \"string1\" is not null."
else
echo "String \"string1\" is null."
fi # Again, gives correct result.
# Still, it is better to quote it ("$string1"), because . . .
string1="a = b"
if [ $string1 ] # Again, $string1 stands unquoted.
then
echo "String \"string1\" is not null."
else
echo "String \"string1\" is null."
fi # Not quoting "$string1" now gives wrong result!
exit 0 # Thank you, also, Florian Wisser, for the "heads-up".
Example 7-7. zmore
|
These are similar to the Bash comparison operators && and ||, used within double brackets.
[[ condition1 && condition2 ]] |
if [ "$expr1" -a "$expr2" ] then echo "Both expr1 and expr2 are true." else echo "Either expr1 or expr2 is false." fi |
But, as rihad points out:
|
No comments:
Post a Comment