# Test variable

**URL:** <https://community.unix.com/t/test-variable/193604>\
**Category:** Shell Programming and Scripting\
**Created:** [April 18, 2008, 3:51am UTC](https://community.unix.com/t/test-variable/193604 "2008-04-18T03:51:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![javeed7](https://community.unix.com/letter_avatar/javeed7/32/6_22734026db12803ebb3e059622ac3534.png) [@javeed7](https://community.unix.com/u/javeed7)\
**Post date:** [April 18, 2008, 3:51am UTC](https://community.unix.com/t/test-variable/193604/1 "2008-04-18T03:51:18Z")

</div>

hi,  
i store a constant in a variable and want to test whether it is M or Z.  
how can i achieve this?? please help

```nohighlight
variable='M'
if [$variable = 'M']
then
ehco 'success'
fi;

```

is this right ??

---

<div class="post-metadata">

**Author:** ![ghostdog74](https://community.unix.com/letter_avatar/ghostdog74/32/6_22734026db12803ebb3e059622ac3534.png) [@ghostdog74](https://community.unix.com/u/ghostdog74)\
**Post date:** [April 18, 2008, 4:00am UTC](https://community.unix.com/t/test-variable/193604/2 "2008-04-18T04:00:07Z")

</div>

> [@javeed7](#):
>
> hi,  
> i store a constant in a variable and want to test whether it is M or Z.  
> how can i achieve this?? please help
> 
> ```plaintext
> variable='M'
> if [$variable = 'M']
> then
> ehco 'success'
> fi;
> 
> ```
> 
> is this right ??

i will let somebody else answer that. i prefer case

```plaintext
case $variable in 
 "M" ) echo "yup " ;;
 * ) echo "nope" ;;
esac

```

---

<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:** [April 19, 2008, 7:59pm UTC](https://community.unix.com/t/test-variable/193604/3 "2008-04-19T19:59:13Z")

</div>

> [@javeed7](#):
>
> hi,  
> i store a constant in a variable and want to test whether it is M or Z.  
> how can i achieve this?? please help
> 
> ```plaintext
> variable='M'
> if [$variable = 'M']
> then
> ehco 'success'
> fi;
> 
> ```
> 
> is this right ??

Like ghostdog, I prefer to use case, but yours will work (almost).

The variable should be quoted or you will get a syntax error if it is empty.

```plaintext
variable=M
if ["$variable" = M]
then
  echo success
fi

#

```
