[net.unix-wizards] In sh, is '[' a portable synonym

rs@mirror.UUCP (10/18/85)

/* Written 10:57 am  Oct 15, 1985 by carl@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 */

abbajay@oracle.UUCP (Dave Abbajay) (10/25/85)

In article <13400016@mirror.UUCP>, rs@mirror.UUCP writes:
> 
> /* Written 10:57 am  Oct 15, 1985 by carl@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

guy@sun.uucp (Guy Harris) (10/27/85)

> '/bin/test' is a *program* and '[]' is intrepreted by the shell in all
> Sys III (and greater) Bourne shells.

Yes, but "test" (as opposed to "/bin/test") is interpreted by the shell in
all Sys III (and greater) Bourne shells!  Yes, this means

	1) you can't run a program in your current directory named "test" by
	   typing "test" - you have to type "./test"

and

	2) even if "/bin" is the first directory in your PATH containing
	   a program called "test", "/bin/test" and "test" do different
	   things.

This may seem strange, but since it's true its strangeness is irrelevant to
the discussion.

	Guy Harris

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (10/27/85)

If your Bourne shell has "[" built-in, it will probably have
"test" built-in, too, so the efficiency argument is moot.

I would like to again make the point that NOTHING anyone does is
going to guarantee that ANY particular code (shell script or not)
will necessarily work on all implementations of UNIX.  The approach
I prefer is to provide as close to the same common environment as
possible everywhere; this reduces porting problems substantially.

west@sdcsla.UUCP (Larry West) (10/30/85)

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

I don't really know which is more readable (though I wonder
why the author was confusing this with the number of keystrokes
-- redundancy often aids readability, and in particular the
spaces around [] are helpful).

However, I would like to point out that users have a habit
of naming programs "test", and of having "/bin" occur late
in their paths.   This tends to cause confusion because the
problem (as usual) doesn't come up until that brief "test"
program has been forgotten.

So, my suggestion is to always use "/bin/test" instead
of simply "test", or else use "[ ]".   I know, fixing the
the particular pathname of a program is a bad idea, but this
seems a fairly safe case (unless you plan to provide an
improved user interface to "test":-).
-- 

Larry West				(USA+619-)452-6771
Institute for Cognitive Science		non-business hours: 452-2256
UC San Diego (mailcode C-015)
La Jolla, CA  92093  U.S.A.

ARPA:	<west@nprdc.ARPA>	or	<west@ucsd.ARPA>
UUCP:	{ucbvax,sdcrdcf,decvax,ihnp4}!sdcsvax!sdcsla!west
  or	{sun,mplvax,gti,ihnss,whuxlb,ulysses}!sdcsla!west

jsdy@hadron.UUCP (Joseph S. D. Yao) (11/09/85)

In article <132@oracle.UUCP> abbajay@oracle.UUCP (Dave Abbajay) writes:
>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.

[Disclaimer:]  I don't have the sV Bourne shell code or the Korn
shell code in front of me; however, I don't remember '[' being
treated as a special command.  I do know that 'test' is linked
to '['.  It does not seem logical to have it both ways, especially
when one can be changed without changing the other.  (Did I  s a y
that everything progrmmers do is logical?)

References: VAX-11/780 running s5r1v1, s5r1v2, s5r2v1, s5r2v2.

(Note:  PLEASE reference the system you're talking about.  I don't
know whether D.A. was talking about vaxen (prob'ly) or one of the
myriad 680X0 systems, or 3B's, or what.)
-- 

	Joe Yao		hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}

radzy@calma.UUCP (Tim Radzykewycz) (11/13/85)

In article <63@hadron.UUCP> jsdy@hadron.UUCP (Joseph S. D. Yao) writes:
>In article <132@oracle.UUCP> abbajay@oracle.UUCP (Dave Abbajay) writes:
>>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.
>
>[Disclaimer:]  I don't have the sV Bourne shell code or the Korn
>shell code in front of me; however, I don't remember '[' being
>treated as a special command.  I do know that 'test' is linked
>to '['.
>References: VAX-11/780 running s5r1v1, s5r1v2, s5r2v1, s5r2v2.

A while back, the sysadm (somewhat junior) of a site I was working
on found the file '/bin/[', and thinking it was a junk file created
when something went wrong, he got rid of it.  All kinds of shell
scripts broke.  This is pretty conclusive evidence that '[' is
*not* treated specially, at least on the 4.1 (vax) system this
was on.

BTW: the sysadm was *me*.
-- 
Tim (radzy) Radzykewycz, The Incredible Radical Cabbage
	calma!radzy@ucbvax.ARPA
	{ucbvax,sun,csd-gould}!calma!radzy