# variable comparison

**URL:** <https://community.unix.com/t/variable-comparison/242044>\
**Category:** Shell Programming and Scripting\
**Created:** [June 8, 2009, 5:29pm UTC](https://community.unix.com/t/variable-comparison/242044 "2009-06-08T17:29:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freakhan](https://community.unix.com/letter_avatar/freakhan/32/6_22734026db12803ebb3e059622ac3534.png) [@Freakhan](https://community.unix.com/u/Freakhan)\
**Post date:** [June 8, 2009, 5:29pm UTC](https://community.unix.com/t/variable-comparison/242044/1 "2009-06-08T17:29:16Z")

</div>

Hello,

I am trying to compare two variables, which both have a word stored in it.  
The code I am using is. Please tell me which one of these is correct.Or please tell me the correct syntax.  
Thankyou

if [$variable1=$variable2] then

if [$($variable1)=$($variable2)] then

if ["$variable1"="$variable2"] then

if ["$variable1" = "$variable2"] then

if ["$variable1" -eq "$variable2"] then

None of them seems to work for me.

---

<div class="post-metadata">

**Author:** ![vidyadhar85](https://community.unix.com/user_avatar/community.unix.com/vidyadhar85/32/997_2.png) [@vidyadhar85](https://community.unix.com/u/vidyadhar85)\
**Post date:** [June 8, 2009, 6:03pm UTC](https://community.unix.com/t/variable-comparison/242044/2 "2009-06-08T18:03:19Z")

</div>

if both variables contain string without space  
if [$var1 = $var2] ; then  
will work  
if they have space put variables in double qoutes

---

<div class="post-metadata">

**Author:** ![Freakhan](https://community.unix.com/letter_avatar/freakhan/32/6_22734026db12803ebb3e059622ac3534.png) [@Freakhan](https://community.unix.com/u/Freakhan)\
**Post date:** [June 8, 2009, 6:09pm UTC](https://community.unix.com/t/variable-comparison/242044/3 "2009-06-08T18:09:54Z")

</div>

Thankyou

---

<div class="post-metadata">

**Author:** ![TonyFullerMalv](https://community.unix.com/user_avatar/community.unix.com/tonyfullermalv/32/1090_2.png) [@TonyFullerMalv](https://community.unix.com/u/TonyFullerMalv)\
**Post date:** [June 8, 2009, 7:08pm UTC](https://community.unix.com/t/variable-comparison/242044/4 "2009-06-08T19:08:15Z")

</div>

> [@](#):
>
> if they have space put variables in double quotes

Also if either variable my be empty then put variables is double quotes:

```plaintext
$ echo ${FRED}

$ FREDA=Freda
$ echo ${FREDA}
Freda
$ if [${FRED} = ${FREDA}]; then echo Equal; else; echo Not equal; fi
bash: [: =: unary operator expected
Not equal
$ if ["${FRED}" = "${FREDA}"]; then echo Equal; else echo Not equal; fi
Not equal
$ 

```
