In sh, is '[' a portable synonym for 'test'?

BALDWIN mike at whuxl.UUCP
Sat Oct 26 13:38:01 AEST 1985


> > > 	if test -f "$1"	<= 15 keystrokes + <CR>
> > > vs.
> > > 	if [ -f "$1" ]	<= 14 keystrokes + <CR>
> > > 
> > > Which is more readable?
> > 
> > The latter, obviously; it reads more like a conditional statement rather
> > than a command.
> 
> Hogwash.  The manual states
> 
> 	if *list* then *list* [ elif *list* then *list* ] ...
> 
> where *list* is defined  as a sequence on one or more pipelines ,i.e
> commands. *Test* is a command like any other, so why should it be treated
> any differently?

Who cares WHAT [ or test really are?  I *like* if statements to look like if
statement, thank you.
	if [ $x = 3 ]
is more pleasing AND READABLE to me than
	if test $x = 3

> > Good grief, do you think that people use the square bracket to save one
> > measly keystroke?  That's not why it's there - it's there to improve the
> > readability of the statement.
> > 
> > 	Guy Harris
> 
> Yes, but only by people who want to obscure the fact that "if tests the
> value returned by the last simple command following it" (S. R. Bourne,
> The UNIX System, page 58).

Oh, bushwah!  I don't want to obscure what the if stmt does, I want to
make it LESS obscure.  If you ask me, "if test expr" is obscuring the
fact that this is a simple conditional, whereas "if [ expr ]" is obvious.

Speaking of test, is anyone else out there annoyed that there really
isn't a good way to deal with strings?  Instead of nice and simple
[ "$x" = foo ] you have to say [ "X$x" = Xfoo ] which is kinda ugly.
Also, [ -n "$x" ] is right out (what if x is "="?), along with -z.
Expr has the same problem with "expr string : regexp".  Bleah!
-- 
						Michael Baldwin
						{at&t}!whuxl!mike



More information about the Comp.unix.wizards mailing list