# if statement negation

**URL:** <https://community.unix.com/t/if-statement-negation/283230>\
**Category:** Shell Programming and Scripting\
**Created:** [February 21, 2011, 5:25pm UTC](https://community.unix.com/t/if-statement-negation/283230 "2011-02-21T17:25:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rethymno19](https://community.unix.com/letter_avatar/rethymno19/32/6_449c533f586939b6e5f246632a46421f.png) [@rethymno19](https://community.unix.com/u/rethymno19)\
**Post date:** [February 21, 2011, 5:25pm UTC](https://community.unix.com/t/if-statement-negation/283230/1 "2011-02-21T17:25:26Z")

</div>

Hi all,

I've been searching the internet and can't find an answer to this. Im trying to figure out how to negate a whole expression befor an if. I'll explain below.

Let's say

x=0  
y=1

[[x = 0 && y = 0]] (this is false) I would like to negate this whole boolean condition. To negate the above expression in other languages I can do something like this

![[x = 0 && y = 0]] returns true

is there something similar in UNIX?

I tried

if ![[x = 0 && y = 0]]; then

............

fi

but this doesn't work it doesn't go into the if statement.

Thanks

---

<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:** [February 21, 2011, 5:38pm UTC](https://community.unix.com/t/if-statement-negation/283230/2 "2011-02-21T17:38:04Z")

</div>

There needs to be a space between the ! and the first [

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [February 21, 2011, 5:44pm UTC](https://community.unix.com/t/if-statement-negation/283230/3 "2011-02-21T17:44:28Z")

</div>

Dont forget the $ in front of variable names and space after !

```nohighlight
if ! [[$x = 0 && $y = 0]]; then

```

or

```nohighlight
if [[! ($x = 0 && $y = 0)]]; then

```

---

<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:** [February 21, 2011, 5:55pm UTC](https://community.unix.com/t/if-statement-negation/283230/4 "2011-02-21T17:55:11Z")

</div>

Also in square brackets in bash and ksh93 double == are preferred for a string comparison in combination with double brackets..

```nohighlight
if ! [[$x == 0 && $y == 0]]; then

```

Numerical comparison:

```nohighlight
if ! [[$x -eq 0 && $y -eq 0]]; then

```

Arithmetic evaluation:

```nohighlight
if ! (( x == 0 && y == 0 )); then

```

Or POSIX tests (works in many other shells):

```nohighlight
if ! ( ["$x" = 0] && ["$y" = 0] ) ; then

```

```nohighlight
if ! ( [$x -eq 0] && [$y -eq 0] ) ; then

```

---

<div class="post-metadata">

**Author:** ![frans](https://community.unix.com/user_avatar/community.unix.com/frans/32/1697_2.png) [@frans](https://community.unix.com/u/frans)\
**Post date:** [February 22, 2011, 1:43am UTC](https://community.unix.com/t/if-statement-negation/283230/5 "2011-02-22T01:43:13Z")

</div>

assuming x and y as positive numeric, this will return true if either x or y != 0 :

```nohighlight
if ((x+y)); then

```
