Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does testing if a string is 'greater' than another work in Bash?

In Bash I can write the following test

[[ "f" > "a" ]] 

which results in returning 0, i.e. true. How does bash actually perform this string comparison? From my understanding > does an integer comparison. Does it try to compare the ASCII value of the operands?

like image 557
helpermethod Avatar asked Aug 16 '12 14:08

helpermethod


People also ask

How do you check if one string is greater than another in bash?

When comparing strings in Bash you can use the following operators: string1 = string2 and string1 == string2 - The equality operator returns true if the operands are equal. Use the = operator with the test [ command. Use the == operator with the [[ command for pattern matching.

How do you check a comparison string?

Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.

How do you compare in bash?

In a Bash script, you'd normally be storing one or both of your strings as variables before comparing them. In this example, we are using the = operator and an if statement to determine if the two strings are equal to each other.

How do you check if two strings are equal in shell script?

Details. Use == operator with bash if statement to check if two strings are equal. You can also use != to check if two string are not equal.


2 Answers

From help test:

  STRING1 > STRING2                  True if STRING1 sorts after STRING2 lexicographically. 

Internally, bash either uses strcoll() or strcmp() for that:

else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0')   {     if (shell_compatibility_level > 40 && flags & TEST_LOCALE)       return ((op[0] == '>') ? (strcoll (arg1, arg2) > 0) : (strcoll (arg1, arg2) < 0));     else       return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0));   } 

The latter actually compares ASCII codes, the former (used when locale is enabled) performs a more specific comparison which is suitable for sorting in given locale.

like image 91
Michał Górny Avatar answered Oct 05 '22 22:10

Michał Górny


It's an alphabetical comparison (AIUI the sort order may be influenced by the current locale). It compares the first character of each string, and if the one on the left has a higher value it's true, if lower it's false; if they're the same, then it compares the second character, etc.

This is not the same as integer comparison, for that you use [[ 2 -gt 1 ]] or (( 2 > 1 )). To illustrate the difference between string and integer comparison, consider that all of the following are "true":

[[ 2 > 10 ]]     # because "2" comes after "1" in ASCII sort order [[ 10 -gt 2 ]]   # because 10 is a larger number than 2 (( 10 > 2 ))     # ditto 

Here are some more test that're true as string comparisons, but would be false with integer comparison:

[[ 05 < 5 ]]    # Because "0" comes before "5" [[ +5 < 0 ]]    # Because "+" comes before the digits [[ -0 < 0 ]]    # Because "-" comes before the digits [[ -1 < -2 ]]   # Because "-" doesn't change how the second character is compared 
like image 31
Gordon Davisson Avatar answered Oct 05 '22 22:10

Gordon Davisson