history question-- Bourne (and C) SHELL COMMENTS

Chris Torek chris at umcp-cs.UUCP
Wed Aug 6 18:53:27 AEST 1986


In article <1751 at ittatc.ATC.ITT.UUCP> yoda at ittatc.ATC.ITT.UUCP
(Todd C. Williams) writes:
>OK, we're from a mixed (SysV, 4bsd, V6, V7) background here, and a
>question has come up which NO ONE is clear on:
>
>	How do you put a comment in a shell script?

The only reliable answer---though hardly satisfactory---is that
you cannot.

>... S.R.Bourne doesn't mention it in his 1978 paper, except for
>the following line from an example script:
>	: 'colon is the comment command'
>Some say that the colon introduces a comment, but that is not entirely true,
>since the line still gets evaluated, at least somewhat.

The colon command is a shell built-in that performs no action and
returns a zero exit code.  Since it is a command, all its arguments
are parsed (though immediately discarded), and the remainder of
the line must be syntactically valid to `sh'.  The easiest way to
ensure that is to enclose it in quotes, as in the example above.

>Others say that it is the sharp/pound/number sign, "#" <--that thing, but
>if you're on a bsd system, you can't START your Bourne shell script with
>it, or the C shell will run your script.  Or should you start with
>	#!/bin/sh
>???  And does the csh fork here, or exec?

Now the picture becomes murkier.

>What is the historical development of the : and the # ?  I am SURE that
>the colon was the character to use in V7, but....

Indeed, a bit of historical perspective is essential.  I am not
certain I will get this right, for I am relatively new to the, er,
`shell games'.  I will leave out 6th Edition entirely---it had a
different shell---and pick up from V7.

In V7 there was The Shell, and The Shell had no Comment Character.
The Null Command `:' was used and all was well.

Along came Berkeley with a PDP-11.  The '11 was slow, and running
all those Null Commands did not help matters a bit.  So the Hackers
came up with a new Shell, the `C Shell', that was supposed to be
faster, and had a Comment Character as well.  And now there was a
Problem.  For when one ran a Shell Script, which Shell was to be
used?  But there was an easy Solution, for the new Shell had a
Comment Character, and it was `#', not `:'.  So one began a C Shell
Script with a C Shell Comment, and all was well.

But as the scripts grew it became clear that `sh' should have a
true Comment Character.  The character `#' was free, and seemed a
logical choice; and it was done.  And now both Shells had Comments,
though `sh' Scripts could not begin with one.  It became a Practise
to begin such Scripts with `:'.

And then one fateful day, it was observed that Only Shells Run
Scripts.  The Wizards were consulted, and one prescribed a cure:
if the Kernel saw a Comment that began with `#!', it could then
interpret the line itself, and thus Run Scripts.  The Shells would
skip the line, for the line was just a Comment.  And so it was
done.

And 2BSD met 32V, and they begat 3BSD, and 3BSD begat 4BSD.  And
so it is that 4BSD Shells have Comments, and the 4BSD Kernel
interprets `#!'.

And now I must leave the storytelling aside, for though there are
those who know of the True History of System V, I am not among
them.  But I believe that by SysIII at the latest, `sh' supported
`#' comments.

In general, when writing a shell script, if you want maximum
portability, and need neither true comments nor direct execution
by the kernel, use the colon (null) command; any post-V6 system
will run such a script with `sh', and everything will work.  If
you are not so concerned with portability, or prefer comments
wherever possible, include them as you wish.  If you write comments
only at the beginnings of lines, less fortunate sites can always
use

	ed - script << end
	g/^#/d
	w
	q
	end
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.unix mailing list