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

carl carl at bdaemon.UUCP
Wed Oct 16 00:57:44 AEST 1985


> > 
> > 	On my 4.2bsd system, the following do the same thing in sh because
> > /bin/[ is a link to /bin/test. ... Is the "[]" form safe to use if you want
> > ....
> 
> V7 systems where /bin/[ isn't a link to /bin/test.  Any V7 or post-V7 system
> can be made to support "if [ <condition> ]" as equivalent to "if test
> .....
> 	GUy Harris

I have absolutely no arguments with Guy's message, but would like to point
out that the [ condition ] construct saves only one keystroke at the
expense of readability.  E.g.:

	if test -f "$1"	<= 15 keystrokes + <CR>
vs.
	if [ -f "$1" ]	<= 14 keystrokes + <CR>

Which is more readable? Note that the spaces around [] are MANDATORY.

Carl Brandauer



More information about the Comp.unix.wizards mailing list