Hello,
was just wondering how to compare strings in unix? I mean as in C there is a function strcmp() in string.h, is there any function in unix for that? I tried using
if [ "p" > "q" ]
and all such variations but didn't succeed. Any help would be appreciated. Thanks in advance
Just a quick note - the string comparison is based on the ASCII value of the characters in the string and may fail for strings containing non-ASCII characters.
Not necessarily true. It will result in an unsatisfactory ordering for certain locales unless the correct collator (for the locale) is provided in the underlying OS to allow locale-sensitive ordering. Bash and other shells do not include built-in collators.
Some shells such as ksh93 and zsh specifically avoid the issue for this reason. From the ksh93 man page
The bash manual states, "lexicographically in the current locale", but in fact uses the character's ASCII value.
The shell does use the locale's collating order for character ranges, e.g., [a-z]. I don't know whether the order is generated by the shell or by the OS.