[prog] bash: string comparisons in conditionals

Conor Daly c.daly at met.ie
Thu Apr 22 09:18:28 EST 2004


On Thu, Apr 22, 2004 at 01:05:37AM +0200 or thereabouts, Riccarda Cassini wrote:
> 
> Conor Daly wrote:
> > 
> > >>>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" ] ?
> > 
> > a = "test test"
> 
> Um, how? - Or do you mean with unquoted $a ?

Yep.  I thought your '[ -z "$a" ]' above was '[ -z $a ]' which will give an
error on the cited input.
 
> 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

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

Conor
-- 
Conor Daly,                   Please avoid sending me 
Met Eireann, Glasnevin Hill,  Word or PowerPoint attachments.
Dublin 9, Ireland             http://www.fsf.org/philosophy/no-word-attachments.html
Ph +3531 8064276 Fax +3531 8064247


**********************************************************************
This e-mail and any files transmitted with it are confidential 
and intended solely for the addressee. If you have received
this email in error please notify the sender.
This e-mail message has also been scanned for the
presence of computer viruses.
**********************************************************************



More information about the Programming mailing list