From: Dr. David Kirkby on
Is this syntax OK

if [ -z $foobar ] ; then

or should it be:

if [ -z "$foobar" ] ; then

Obviously the latter will not fail, but someone has said the former
can generate a syntax error. I'm not sure if he is right or not.

Dave
From: Janis Papanagnou on
Dr. David Kirkby schrieb:
> Is this syntax OK
>
> if [ -z $foobar ] ; then
>
> or should it be:
>
> if [ -z "$foobar" ] ; then
>
> Obviously the latter will not fail, but someone has said the former
> can generate a syntax error. I'm not sure if he is right or not.

Certainly he's right.

$ if [ -z $foobar ] ; then echo T ; else echo F ; fi
T

$ foobar=Hello
$ if [ -z $foobar ] ; then echo T ; else echo F ; fi
F

$ foobar="Hello World"
$ if [ -z $foobar ] ; then echo T ; else echo F ; fi
bash: [: Hello: binary operator expected
F

$ if [ -z "$foobar" ] ; then echo T ; else echo F ; fi
F


Janis

>
> Dave
From: Daniel Molina Wegener on
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On Mi� 21 Jul 2010 03:40,
Dr. David Kirkby wrote:

> Is this syntax OK
>
> if [ -z $foobar ] ; then
>
> or should it be:
>
> if [ -z "$foobar" ] ; then

This is the right way.

>
> Obviously the latter will not fail, but someone has said the former
> can generate a syntax error. I'm not sure if he is right or not.

You can test the same expression if -z is absent, by using

if [ "x" = "x$foobar" ]; then

So, if $foobar is empty, the expression will be true. Take a look
on configure files generated from autoconf to give more details on
portable commands.

>
> Dave

Best regards,
- --
Daniel Molina Wegener <dmw [at] coder [dot] cl>
System Programmer & Web Developer
Phone: +56 (2) 979-0277 | Blog: http://coder.cl/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iQIcBAEBCgAGBQJMRtg7AAoJEHxqfq6Y4O5N/pgQAIgCUuxqYVyna6v9G8pcvepO
xlXMyms7SUO/j5ZVJ+Tf3t/XuLBMZfH+iarb72EDhNS5AZODuA03M7R18bTP8jxQ
tHkxCgbbeTxz6dY7yIYdLOaaGSTuewnJUaMcWeVNR/iyjivK9902E5tPxSDwk3R6
uuZm8nGSRO4cUdgLZD8Df56GrK2dvmKSYPCDa0R7xdhhzlWjJQN0HPRUl7FXjFKg
aWh9jBF6ovWlpIUu7RXsrGq1YQBqR6/lnOnamo8B1eE3Eg5mavSmyFLbN2VX6Vjx
VdrIctdzlRbyHAs5rg/yFUfBP8lAqvSlNMaHVN8UPvMeUjDhflUo+GfG3uYcQtJt
89XqBZb5i8MGOlg/PD1Vi7jp0z9yNm9n6pWxjc9rIDygIe/J2EAGjzqmsUdT/wji
q9zEHoSyl6+N6dhLaSKDWCRQw6QDtm/AI2dotA0hg03drBJtNGjKDWfpQVL3w/MA
iujBGTtTuhDP/kv+cyDuwX5/n2iCpdeOmOUBAZWbuWvm113ZtRHc8JDl3dlREjrX
8sfwMR2WFeRrx6kC9QVSj6TabjIygwRvNpkyQapbxPBXd9WALbOJyqSns3hMrL6w
BC2Kex5AFOrBrWq3BS8ph5fI0fUGqRg8SisuUh7I/5hBinkbab0buChn+YgvDyoS
B5CIV9RE+kCWE0L7TyJe
=T2b5
-----END PGP SIGNATURE-----

From: Dr. David Kirkby on
On Jul 21, 8:50 am, Janis Papanagnou <janis_papanag...(a)hotmail.com>
wrote:
> Dr. David Kirkby schrieb:
>
> > Is this syntax OK
>
> > if [ -z $foobar ] ; then
>
> > or should it be:
>
> > if [ -z "$foobar" ] ; then
>
> > Obviously the latter will not fail, but someone has said the former
> > can generate a syntax error. I'm not sure if he is right or not.
>
> Certainly he's right.
>
> $ if [ -z $foobar ] ; then echo T ; else echo F ; fi
> T
>
> $ foobar=Hello
> $ if [ -z $foobar ] ; then echo T ; else echo F ; fi
> F
>
> $ foobar="Hello World"
> $ if [ -z $foobar ] ; then echo T ; else echo F ; fi
> bash: [: Hello: binary operator expected
> F
>
> $ if [ -z "$foobar" ] ; then echo T ; else echo F ; fi
> F
>
> Janis
>
>
>
> > Dave

Thank you very much. I've now corrected my script

Dave