/bin/sh feature?

Guy Harris guy at rlgvax.UUCP
Tue Jul 10 11:55:31 AEST 1984


> The problem is that sh parses comment lines starting with a ":" (don't
> ask me why...) and waits for the closing "'".

The reason is simple - a line starting with a ":" isn't a comment in the
sense that most programming languages use the word "comment".  ":" is a
built-in command in the shell that does nothing.  Think of it as equivalent
to "echo >/dev/null 2>&1".  It returns an "exit status" of 0 (true).  As
such, it's useful when doing "while true" (since it's built-in, it's faster
than "true" which is a shell file) and useful when you want something like

	if ! cmp -s $1 $2
	then
		diff $1 $2
	fi

which compares two files and, if unequal, "diff"s them.  Unfortunately, you
can't negate the exit status of a "list" in an "if", so you have to do
something like

	if cmp -s $1 $2
	then
		:
	else
		diff $1 $2
	fi

(Yes, I know you could run the "cmp" and then do a

	if test $? -ne 0

but that's *UG*ly.)

Fortunately, every Bourne shell except the V7 shell supports *real* comments,
done with "#".  Everything following the "#" on the line is ignored, even by
the shell's lexical analyzer.

	Guy Harris
	{seismo,ihnp4,allegra}!rlgvax!guy



More information about the Comp.unix.wizards mailing list