home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / question / 11050 < prev    next >
Encoding:
Internet Message Format  |  1992-09-14  |  3.4 KB

  1. Path: sparky!uunet!mcsun!uknet!acorn!ixi!ixi!pd
  2. From: pd@x.co.uk (Paul Davey)
  3. Newsgroups: comp.unix.questions
  4. Subject: Re: ':' line in scripts [LONGish]
  5. Message-ID: <PD.92Sep14184828@herts.x.co.uk>
  6. Date: 14 Sep 92 18:48:28 GMT
  7. References: <1992Sep11.163325.7096@qcinet.Gwinnett.COM>
  8. Sender: paul@x.co.uk (Paul Davey)
  9. Organization: IXI Ltd.
  10. Lines: 85
  11. In-Reply-To: dpayton@qcinet.Gwinnett.COM's message of 11 Sep 92 16:33:25 GMT
  12.  
  13. >>>>> On 11 Sep 92 16:33:25 GMT, dpayton@qcinet.Gwinnett.COM (Doug Payton) said:
  14.  
  15. Doug> I've seen, on occasion, scripts that begin with the line
  16.  
  17. Doug> :
  18.  
  19. Doug> which, as I recall, is simply 'true'.  Sometimes I've seen
  20.  
  21. Doug> #!/bin/sh
  22. Doug> :
  23.  
  24. Doug> ...but the ':' is still there.
  25.  
  26. Doug> What is it's purpose?  Does it init the shell to a known state?
  27.  
  28.  
  29. From sh(1)
  30.  
  31. :    No effect; the command does nothing.  A zero exit code is returned.
  32.                
  33.  
  34. Once upon a time, many releases ago, (when the epoch was a recent event)
  35. Unix machines introduced the idea of exectable text files.
  36.  
  37. This was possible on other operating systems of the time, but Unix
  38. allowed not just a command such as ``sh foo'' but  ``foo'' when foo  
  39. was a file containing text, but was not a binary exectable.
  40.  
  41. Early shells had no comment character but the ``:'' could be used to
  42. almost do this. (One had to be wary of redirections and substitutions
  43. which would be still be carried out in ``comment'' lines such as -
  44.  
  45. : This is my new program , try it with foo > filename
  46.  ... (script)
  47.  
  48. or more usefully
  49.  
  50. : This exits if this variable substitution fails ${user?}
  51.  
  52. Then those useful people at UCB wrote a new shell, with an
  53. (alleged) syntax similar to C.
  54.  
  55. The C shell (as it was imaginatively called) introduced a wealth of
  56. good things* including a comment character ``#''. Scripts starting
  57. with this character could therefore be executed by csh.
  58.  
  59.     [* see footnote]
  60.  
  61. Scripts were passed to the Bourne shell if some other character was
  62. seen first and so ``:'' was often used. This behaviour survives (on
  63. BSD based machines) even to this day when the Bourne shell recognises
  64. ``#'' as a comment itself.
  65.  
  66. Systems differ as to whether an initial ``#'' signifies a (default)
  67. Bourne shell or a C shell script. This means that Bourne shell scripts
  68. called from an interactive C shell may be run erroneously by the C
  69. shell.
  70.  
  71.     Note: It is possible to override the meaning of a single initial
  72.       ``#'' in the C shell by saying:
  73.  
  74.         ``set shell = /bin/sh''
  75.  
  76.     This is *not* the same as:
  77.  
  78.          ``setenv SHELL /bin/sh''.
  79.  
  80.  
  81. Later still some bright young sparc(?) decided to allow any command
  82. intrepreter to be specified as the first line of a script file.
  83.  
  84. Thus the syntax for ``#!/bin/prog'' or ``#!prog'' was
  85. introduced, where prog could be any program able to read
  86. commands from the rest of the file.
  87.  
  88. Amoung the other good things from UCB was a new filesystem and
  89. kernal the latter of which was adjusted to recognise the ``#!''
  90. syntax and would execute the appropriate script.
  91.  
  92. Sadly certain other vendors have taken a long long time to even begin
  93. to introduce this handy syntax and so Bourne shell scripts are
  94. sometimes still written with a first line of ``:'' to ensure that they
  95. are executed by the Bourne shell.
  96.  
  97. Footnote: Readers who wish to mention ksh, bash or zsh 
  98.       here are firmly directed to /dev/null.
  99.  
  100.  
  101. --
  102.  Regards,              pd@x.co.uk           IXI Ltd.
  103.     Paul Davey          pd@ixi.uucp          62-74 Burleigh St.
  104.                   ...!uunet!ixi!pd     Cambridge,  U.K.
  105.  "These _are_ interesting times"  +44 223 462 131      CB1  1OJ
  106.