The difference in the Bourne shell between :
and #
I don't know how people learn the Bourne shell these days, but when I
learned it, I first encountered ':
' as a vaguely peculiar second way
of writing comments, one used by some old shell scripts that hung around
our systems. This is true as far as it goes, which is not necessarily
very far.
The difference between #
and :
is that #
starts a real comment
that runs to the end of the line, while :
is a real command that
just does nothing (this is what makes the :;
prompt trick work). As a real command, its 'arguments'
are parsed and things in it can have potential side effects, which means
that you have to be careful what you put after it. However, this also
means that it can be used in places that require an actual command.
In the old days the classical example of this was in if
statements,
because the Bourne shell had not yet picked up a general negation
operator. Instead you had to write:
if something ....; then : do nothing else # do the interesting thing fi
You couldn't use a #
comment in place of the :
, because the Bourne
shell grammar rules require that there be a statement (and thus a
command).
(Modern spec compliant Bourne shells have the general negation
operator '!
', so you can express this directly.)
There are obscure uses for :
in other contexts; for example, if
you want an infinite while
loop, the best way to write it is:
while : ; do <whatever>; done
This has the same effect as using, say, true
, but usually has less
overhead.
(Commenting things in the Bourne shell has a complicated history that does not fit in this entry.)
|
|