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

carl carl at bdaemon.UUCP
Fri Oct 25 02:47:10 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?

> It may be *implemented* as a command (which, if the command
> isn't builtin, slows it down - somebody who complained that "test" shouldn't
> be built in was later seen using a "case" statement instead of an "if" and a
> "test" in order to make it run faster), but that fact isn't relevant to
> understanding what it *does*.

Agreed, irrelevant to the present discussion.

> 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).

Carl Brandauer



More information about the Comp.unix.wizards mailing list