[prog] bash: string comparisons in conditionals

Riccarda Cassini riccarda.cassini at gmx.de
Thu Apr 22 13:49:33 EST 2004


Conor Daly wrote:
> On Thu, Apr 22, 2004 at 01:05:37AM +0200 or thereabouts, Riccarda Cassini
> wrote:
> > I seem to be getting the same correct results for all kinds of input
> > with all of the following variations:
> > 
> >   [ ! "$a" ]
> >   [ -z "$a" ]
> >   [ "$a" = "" ]
> >   [ x"$a" = x ]
> >   [ "x$a" = x ]
> >   [ "x$a" = "x" ]
> 
> BTW, a little something to add to your arsenal: 
> 
> How does the shell distinguish between the variable 'ax' and the variable
> 'a' followed by the letter 'x'?
> 
> $ax
> "$a"x
> $a"x"
> ${a}x

I guess if I'd work out all functionally equivalent permutations, I'd
end up with at least 16 valid tests for 'string being empty', not
counting symmetrical '='-versions, '==', and letters other than 'x'
(but allowing single quotes around the x)...
Who said that perl is the TMTOWTDI language? ;-)


> The braces delimit a variable name to allow non-whitespace characters to
> follow.

good to know - works the same way in perl, btw.

Thanks Conor,

Riccarda



-- 
"Sie haben neue Mails!" - Die GMX Toolbar informiert Sie beim Surfen!
Jetzt aktivieren unter http://www.gmx.net/info



More information about the Programming mailing list