[prog] bash: string comparisons in conditionals

Daniel lc-programming at nada.refused.cc
Thu Apr 22 01:26:38 EST 2004


On Mon, Apr 19, 2004 at 05:02:13PM +0200, Riccarda Cassini wrote:
> When looking at other people's scripts, I sometimes see constructs like
> 
>   if [ "x$1" = "x" ] 
> 
> I understand that adding identical substrings to both sides of the
> comparison doesn't really change the outcome of the test, but why is
> this necessary? Or, if it isn't necessary, why are many people (which

Well, [ "$1" = "" ] and [ -z "$1"] is semantically the same, but
some people prefer the first way, because it's more readable.

Afair, the strings are prefixed with "x", to be compatible with older
(very old) versions of the test programm, which used to fail with
[ "" = "" ].

						-Daniel-



More information about the Programming mailing list