# Problem if statement

**URL:** <https://community.unix.com/t/problem-if-statement/344430>\
**Category:** Shell Programming and Scripting\
**Created:** [July 14, 2014, 2:44am UTC](https://community.unix.com/t/problem-if-statement/344430 "2014-07-14T02:44:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhas85](https://community.unix.com/letter_avatar/bhas85/32/5_5575768a8748004e209b776fc1b2916d.png) [@bhas85](https://community.unix.com/u/bhas85)\
**Post date:** [July 14, 2014, 2:44am UTC](https://community.unix.com/t/problem-if-statement/344430/1 "2014-07-14T02:44:25Z")

</div>

```nohighlight
echo "Enter the variable: " "
read var1
echo " "
for i in ib eb atm
do
if [$var1 = $i]; then
mv properties environment.properties
break
else
 echo "No changes to $var1 "
fi
done

```

When i run and enter the eb it's not working.Any suggestions please..

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [July 14, 2014, 3:22am UTC](https://community.unix.com/t/problem-if-statement/344430/2 "2014-07-14T03:22:29Z")

</div>

What do you mean by "it's not working"? Do you mean you're getting a syntax error for mismatched quotes on the first line of your script?

What are you expecting to happen?

What is happening?

What OS are you using?

What shell are you using?

---

<div class="post-metadata">

**Author:** ![bhas85](https://community.unix.com/letter_avatar/bhas85/32/5_5575768a8748004e209b776fc1b2916d.png) [@bhas85](https://community.unix.com/u/bhas85)\
**Post date:** [July 14, 2014, 5:36am UTC](https://community.unix.com/t/problem-if-statement/344430/3 "2014-07-14T05:36:17Z")

</div>

1)I am using AIX operating system.  
2) I am using bash shell  
3)Entered value is ib or eb or atm then it should mv properties environment.properties else it should not exit from the script. It should move next step in the script.

I didn't see any error messages but my logic is not working. When I compare if [$var1 = $i]

Var1= eb it should mv the file but it is going else place.

Enter the variable:  
eb

o/p:  
No changes to eb

---

<div class="post-metadata">

**Author:** ![SriniShoo](https://community.unix.com/letter_avatar/srinishoo/32/5_5575768a8748004e209b776fc1b2916d.png) [@SriniShoo](https://community.unix.com/u/SriniShoo)\
**Post date:** [July 14, 2014, 6:22am UTC](https://community.unix.com/t/problem-if-statement/344430/4 "2014-07-14T06:22:54Z")

</div>

```nohighlight
echo "Enter the variable: "
read var1
echo " "
for i in ib eb atm
do
if [[$var1 == $i]]; then
mv properties environment.properties
break
else
 echo "No changes to $var1 "
fi
done

```

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/5_5575768a8748004e209b776fc1b2916d.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [July 14, 2014, 6:44am UTC](https://community.unix.com/t/problem-if-statement/344430/5 "2014-07-14T06:44:32Z")

</div>

If you enter eb  
then it prints 'No changes' for i = ib, but then runs the mv for i = eb

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/5_5575768a8748004e209b776fc1b2916d.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [July 14, 2014, 6:45am UTC](https://community.unix.com/t/problem-if-statement/344430/6 "2014-07-14T06:45:41Z")

</div>

> [@bhas85](#):
>
> ```plaintext
> if [$var1 = $i]; then
> 
> ```

There are several problems with this:

first, `test` s string comparison operator is not "=" but "==", as SriniShoo has already stated;

second, you should quote your variables: if "$var" contains whitespace, your command is going to fail with a syntax error otherwise:

```plaintext
if ["$var1" == "$i"]; then

```

In addition your logic is flawed: the for-loop will try one possible value after the next and for every failed attempt print "no changes". Hence, if you enter "atm" the output will read something like:

```plaintext
No changes to atm
No changes to atm

```

and your file will get moved - or not, depending on it being there in first place, otherwise you will get an error.

You might want to make the output of your script somewhat better understandable.

I hope this helps.

bakunin

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [July 14, 2014, 7:01am UTC](https://community.unix.com/t/problem-if-statement/344430/7 "2014-07-14T07:01:51Z")

</div>

A better way of doing this would be:

```plaintext
case $var1 in 
  (ib|eb|atm) echo match ;;
  (*) echo no match ;;
esac

```

> [@bakunin](#):
>
> [..]  
> first, `test` s string comparison operator is not "=" but "==", as SriniShoo has already stated;

Actually, it is a single `=` . See [test: operands](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128_05)

---

<div class="post-metadata">

**Author:** ![bhas85](https://community.unix.com/letter_avatar/bhas85/32/5_5575768a8748004e209b776fc1b2916d.png) [@bhas85](https://community.unix.com/u/bhas85)\
**Post date:** [July 15, 2014, 5:53am UTC](https://community.unix.com/t/problem-if-statement/344430/8 "2014-07-15T05:53:32Z")

</div>

Thanks a lot Scrutinizer.It's working as expected..
