In sh, is '[' a portable synonym

Dave Abbajay abbajay at oracle.UUCP
Sat Oct 26 05:36:49 AEST 1985


In article <13400016 at mirror.UUCP>, rs at mirror.UUCP writes:
> 
> /* Written 10:57 am  Oct 15, 1985 by carl at bdaemon in mirror:net.unix-wizar */
> /* ---------- "Re: In sh, is '[' a portable synony" ---------- */
> > > 
> > > 	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
> /* End of text from mirror:net.unix-wizar */

I also have no REAL arguments against Guy's comments save one. '/bin/test'
is a *program* and '[]' is intrepreted by the shell in all Sys III (and
greater) Bourne shells. This could have significant impact on shell script
performance, obviously, especially where loops are concerned. I was not
aware that a link between /bin/test and /bin/[ would make the '[]'
construct work in shells where '[]' was not supported! In my way of thinking,
this makes '[]' even MORE desirable for portability reasons (if the
link can be made on the target machine) as then you only pay the additional
overhead of the 'execs' of "test\[" on those machines where the shell doesn't
support '[]'.

And saving a keystroke for every test condition can be a desirable thing to
do, especially with some of the shell scripts I've written :-).


-- 
Dave Abbajay
Senior Technical Staff
ORACLE Corporation
(415)854-7350                                    hplabs!oracle!abbajay



More information about the Comp.unix.wizards mailing list