# NULL checking

**URL:** <https://community.unix.com/t/null-checking/296962>\
**Category:** Shell Programming and Scripting\
**Created:** [October 11, 2011, 9:07am UTC](https://community.unix.com/t/null-checking/296962 "2011-10-11T09:07:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ananthdoss](https://community.unix.com/letter_avatar/ananthdoss/32/6_22734026db12803ebb3e059622ac3534.png) [@Ananthdoss](https://community.unix.com/u/Ananthdoss)\
**Post date:** [October 11, 2011, 9:07am UTC](https://community.unix.com/t/null-checking/296962/1 "2011-10-11T09:07:49Z")

</div>

Hi I need to check the output of a string to null. For eg the output I get for $KIT is empty. So want to echo something when the output is empty.  
How can I do that?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Skrynesaver](https://community.unix.com/user_avatar/community.unix.com/skrynesaver/32/14143_2.png) [@Skrynesaver](https://community.unix.com/u/Skrynesaver)\
**Post date:** [October 11, 2011, 9:10am UTC](https://community.unix.com/t/null-checking/296962/2 "2011-10-11T09:10:52Z")

</div>

```nohighlight
if ["X$KIT" =="X"] ; then
   echo Kit is undefined
fi

```

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [October 11, 2011, 9:18am UTC](https://community.unix.com/t/null-checking/296962/3 "2011-10-11T09:18:39Z")

</div>

Or:

```nohighlight
[-z "$KIT"] && your code when empty

```

---

<div class="post-metadata">

**Author:** ![clx](https://community.unix.com/letter_avatar/clx/32/6_22734026db12803ebb3e059622ac3534.png) [@clx](https://community.unix.com/u/clx)\
**Post date:** [October 11, 2011, 9:48am UTC](https://community.unix.com/t/null-checking/296962/4 "2011-10-11T09:48:16Z")

</div>

Just `["$KIT"] ` should also work I guess!?

---

<div class="post-metadata">

**Author:** ![Skrynesaver](https://community.unix.com/user_avatar/community.unix.com/skrynesaver/32/14143_2.png) [@Skrynesaver](https://community.unix.com/u/Skrynesaver)\
**Post date:** [October 11, 2011, 9:50am UTC](https://community.unix.com/t/null-checking/296962/5 "2011-10-11T09:50:45Z")

</div>

> [@anchal\_khare](#):
>
> Just `["$KIT"] ` should also work I guess!?

Not if `kit=0`

---

<div class="post-metadata">

**Author:** ![clx](https://community.unix.com/letter_avatar/clx/32/6_22734026db12803ebb3e059622ac3534.png) [@clx](https://community.unix.com/u/clx)\
**Post date:** [October 11, 2011, 9:55am UTC](https://community.unix.com/t/null-checking/296962/6 "2011-10-11T09:55:27Z")

</div>

Yeah. But in that case its not "null".

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [October 11, 2011, 10:06am UTC](https://community.unix.com/t/null-checking/296962/7 "2011-10-11T10:06:16Z")

</div>

Yep,  
with most modern shells.

It may not work with the old Bourne shell in some corner cases:

```plaintext
bash-2.03$ uname -sr
SunOS 5.8
bash-2.03$ sh -c 'v=-d; ["$v"] && echo ok'
sh: test: argument expected
bash-2.03$ bash -c 'v=-d; ["$v"] && echo ok'
ok
bash-2.03$ ksh -c 'v=-d; ["$v"] && echo ok'
ok

```

See [bit.ly/pjR2c6](http://bit.ly/pjR2c6) for more:

```plaintext
The two commands:

  test "$1" 
  test ! "$1"    

could not be used reliably on some historical systems. Unexpected results would occur if such a string expression 
were used and $1 expanded to '!', '(', or a known unary primary. Better constructs are:

  test -n "$1"
  test -z "$1" 

```
