Why do people use: if [ "x$FOO" = "x" ] .... ?

Bill Stewart HO 4K-437 x0705 wcs at homxb.UUCP
Fri Apr 19 10:32:04 AEST 1985


Last week I posted an article asking why people use constructs
like the above instead of if [ -z "$FOO" ] or if [ "Known" = "$FOO" ]
Yeah, I should have worried about the case where $FOO has a value
special to test, such as "-z" or "=".  The most interesting reply
pointed out that the answers were different on 4.2BSD.

For both ksh and SysV /bin/sh, the = operator has higher precedence
than -z or -n.  For 4.2 BSD it doesn't.
	if [ -z = -z ] ; then echo true ; else echo false ; fi
On SysV and ksh, this prints "true"; on 4.2BSD it prints "false".
On the other hand,
	if [ -z = ] ; then echo true ; else echo false; fi
On 4.2BSD this prints "false"; on ksh and SysV it prints
	"sh: test: argument expected"

So, those leftover-from-v6 constructs with the "x$FOO" are
still useful.
		Bill Stewart, ho95c!wcs
-- 
"The {first,last} major program written in ADA will be a COBOL interpreter."
					Stewart, 1984
Bill Stewart
AT&T Bell Labs, Holmdel NJ
HO 4K-435 x0705   (201-949-0705)
ho95b!wcs
ucbvax!ihnp4!ho95b!wcs
decvax1harpo!ho95b!wcs



More information about the Comp.unix mailing list