[prog] bash: string comparisons in conditionals

Jimen Ching jching at flex.com
Mon Apr 19 13:51:37 EST 2004

On Mon, 19 Apr 2004, Riccarda Cassini wrote:
>  null == undefined == not initialized == not existing :=
>                                 variable doesn't exist at all
>                                 or hasn't been assigned anything yet
>  empty := string that doesn't contain anything (zero-length)
>  zero  := numeric value '0'
>Is that correct?

There's no one true definition for any of these.  In some contexts, one
can be substituted for another.

>Last question: are there any circumstances in which [ "$a" = "" ]
>(or [ "x$a" = "x" ] for that matter) would not give the same result as
>[ -z "$a" ] ?

An empty string has length zero, so no.

>Sub-question: are quotes around the $a recommended in the latter case,

It depends.  My manual page says -z takes a string as argument.  So
whether you need qoutes depends on whether 'a' is a string or something
else.  I.e. if 'a' is a number, then yes.

Jimen Ching (WH6BRR)      jching at flex.com     wh6brr at uhm.ampr.org

More information about the Programming mailing list