# problem in string comparison in shell programming

**URL:** <https://community.unix.com/t/problem-in-string-comparison-in-shell-programming/233025>\
**Category:** Shell Programming and Scripting\
**Created:** [February 25, 2009, 10:34am UTC](https://community.unix.com/t/problem-in-string-comparison-in-shell-programming/233025 "2009-02-25T10:34:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![salman4u](https://community.unix.com/user_avatar/community.unix.com/salman4u/32/4752_2.png) [@salman4u](https://community.unix.com/u/salman4u)\
**Post date:** [February 25, 2009, 10:34am UTC](https://community.unix.com/t/problem-in-string-comparison-in-shell-programming/233025/1 "2009-02-25T10:34:53Z")

</div>

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

```nohighlight
    if ["p" > "q"]

```

and all such variations but didn't succeed. Any help would be appreciated. Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![Shahul](https://community.unix.com/user_avatar/community.unix.com/shahul/32/1043_2.png) [@Shahul](https://community.unix.com/u/Shahul)\
**Post date:** [February 25, 2009, 11:33am UTC](https://community.unix.com/t/problem-in-string-comparison-in-shell-programming/233025/2 "2009-02-25T11:33:24Z")

</div>

Hi,

you can compare string..like below..

if [$valuea -ge $valueb]  
then  
command  
else  
command  
fi

to compare two strings..

you can use lessthan --\> -le  
greater than -ge  
equal -eq

Thanks  
Sha

---

<div class="post-metadata">

**Author:** ![avi.skynet](https://community.unix.com/letter_avatar/avi.skynet/32/5_5575768a8748004e209b776fc1b2916d.png) [@avi.skynet](https://community.unix.com/u/avi.skynet)\
**Post date:** [February 25, 2009, 11:36am UTC](https://community.unix.com/t/problem-in-string-comparison-in-shell-programming/233025/3 "2009-02-25T11:36:46Z")

</div>

hi  
you can compare the string by the following commands as u have told strcmp() in C  
if ["$string1" == "$string2"]  
then

else

fi

---

<div class="post-metadata">

**Author:** ![cfajohnson](https://community.unix.com/user_avatar/community.unix.com/cfajohnson/32/16669_2.png) [@cfajohnson](https://community.unix.com/u/cfajohnson)\
**Post date:** [February 25, 2009, 1:55pm UTC](https://community.unix.com/t/problem-in-string-comparison-in-shell-programming/233025/4 "2009-02-25T13:55:25Z")

</div>

> [@salman4u](#):
>
> 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
> 
> ```plaintext
> if ["p" > "q"]
> 
> ```
> 
> and all such variations but didn't succeed. Any help would be appreciated. Thanks in advance 🙂

In bash, you can compare alphabetically, but it is not standard. You need to escape the angle bracket:

```plaintext
if [p \> q]

```

---

<div class="post-metadata">

**Author:** ![salman4u](https://community.unix.com/user_avatar/community.unix.com/salman4u/32/4752_2.png) [@salman4u](https://community.unix.com/u/salman4u)\
**Post date:** [February 26, 2009, 5:05am UTC](https://community.unix.com/t/problem-in-string-comparison-in-shell-programming/233025/5 "2009-02-26T05:05:25Z")

</div>

> [@shahul](#):
>
> Hi,
> 
> you can compare string..like below..
> 
> if [$valuea -ge $valueb]  
> then  
> command  
> else  
> command  
> fi
> 
> to compare two strings..
> 
> you can use lessthan --\> -le  
> greater than -ge  
> equal -eq
> 
> Thanks  
> Sha

I dont think you can compare strings with -le -gt -ge. It's meant for only integer comparison.

---

<div class="post-metadata">

**Author:** ![salman4u](https://community.unix.com/user_avatar/community.unix.com/salman4u/32/4752_2.png) [@salman4u](https://community.unix.com/u/salman4u)\
**Post date:** [February 26, 2009, 5:06am UTC](https://community.unix.com/t/problem-in-string-comparison-in-shell-programming/233025/6 "2009-02-26T05:06:10Z")

</div>

> [@cfajohnson](#):
>
> In bash, you can compare alphabetically, but it is not standard. You need to escape the angle bracket:
> 
> ```plaintext
> if [p \> q]
> 
> ```

cfajohnson 🙂 You're the man. Thanks!!

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [February 26, 2009, 9:12am UTC](https://community.unix.com/t/problem-in-string-comparison-in-shell-programming/233025/7 "2009-02-26T09:12:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cfajohnson](https://community.unix.com/user_avatar/community.unix.com/cfajohnson/32/16669_2.png) [@cfajohnson](https://community.unix.com/u/cfajohnson)\
**Post date:** [February 26, 2009, 12:34pm UTC](https://community.unix.com/t/problem-in-string-comparison-in-shell-programming/233025/8 "2009-02-26T12:34:44Z")

</div>

> [@fpmurphy](#):
>
> 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.

It should work for any values. It compares "lexicographically in the current locale".

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [February 27, 2009, 12:12am UTC](https://community.unix.com/t/problem-in-string-comparison-in-shell-programming/233025/9 "2009-02-27T00:12:33Z")

</div>

> [@](#):
>
> It should work for any values. It compares "lexicographically in the current locale".

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

> [@](#):
>
> string1 \< string2  
> True, if string1 comes before string2 based on ASCII value of their characters.  
> string1 \> string2  
> True, if string1 comes after string2 based on ASCII value of their characters.

The zsh manual has almost identical text.

---

<div class="post-metadata">

**Author:** ![cfajohnson](https://community.unix.com/user_avatar/community.unix.com/cfajohnson/32/16669_2.png) [@cfajohnson](https://community.unix.com/u/cfajohnson)\
**Post date:** [February 27, 2009, 9:02pm UTC](https://community.unix.com/t/problem-in-string-comparison-in-shell-programming/233025/10 "2009-02-27T21:02:54Z")

</div>

> [@fpmurphy](#):
>
> 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 zsh manual has almost identical text.

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.
