history question-- Bourne (and C) SHELL COMMENTS
Henry Spencer
henry at utzoo.UUCP
Thu Aug 7 07:29:00 AEST 1986
> 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?
If you've got a mixture of new, old, and ancient shells, you're out
of luck: there is no single entirely satisfactory way.
> Some say that the colon introduces a comment, but that is not entirely true,
> since the line still gets evaluated, at least somewhat. So what is the
> purpose of the colon? It's not a comment, but it is????
The shell's command parser and preparation phases don't know that colon is
anything special, so they treat it as an ordinary command. The execution
phase knows that colon is a special command which does nothing. So you
can put anything you want in colon's arguments, and it will be ignored,
*if* it has no special significance to the earlier phases. This is
not entirely satisfactory because it means that things like ">" cannot
appear unless they are backslashed or quoted, and things like "'" get
very tricky.
> 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.
In modern shells, # at the beginning of a "word" means that the rest of
the line is a comment. This is the new standard convention. Unfortunately,
some old shells don't cope properly. In particular, old broken C shells
think a # at the beginning means "C shell script". The fix to this is
to get a modern C shell.
> Or should you start with
> #!/bin/sh
> ??? And does the csh fork here, or exec?
In recent Unix versions, "#!" at the very start of a file is handled
specially by the kernel, and the C shell never gets involved if the
command specified is "/bin/sh". "#!/bin/sh" tells the kernel itself to
fire up a copy of /bin/sh and run the rest of the file into it. It's a
general way to specify the interpreter for a command file.
> What is the historical development of the : and the # ? I am SURE that
> the colon was the character to use in V7, but....
The # arrived in the Bourne shell shortly after V7; I'm not sure of its
history in the C shell. : was the old way to do things, with # introduced
partly because : was so unsatisfactory. As an added bonus, by the way,
# is a good deal more efficient, since the shell isn't wasting a lot of
preparation on the do-nothing : command.
> There are several methods to use here. Which is the one of choice? and why?
If you must cope with old systems, either ones that don't recognize # or
ones with C shells that misinterpret it, you must use : for comments.
Otherwise, you should use # because it's less hassle and more efficient.
You should probably use #!/bin/sh at the start of shell files, since it's
harmless in systems whose kernels don't know about "#!" and useful in
those that do.
--
Henry Spencer @ U of Toronto Zoology
{allegra,ihnp4,decvax,pyramid}!utzoo!henry
More information about the Comp.unix.wizards
mailing list