[net.unix] Comment lines in the Bourne shell

jwp@uwmacc.UUCP (06/10/85)

I've noticed that on my system (2.8BSD) the Bourne shell
comment character doesn't do what I expected.  The shell
still looks at some characters on the line, even though
the line is commented out.  For example, the comment

: to use this file type <name> | lpr

produces a message referring to the pipe, even though the whole
line is commented out.  What am I missing?

-- 
	Jeff Percival ...!uwvax!uwmacc!jwp

guy@sun.uucp (Guy Harris) (06/11/85)

> I've noticed that on my system (2.8BSD) the Bourne shell
> comment character doesn't do what I expected.  The shell
> still looks at some characters on the line, even though
> the line is commented out.  For example, the comment
> 
> : to use this file type <name> | lpr
> 
> produces a message referring to the pipe, even though the whole
> line is commented out.  What am I missing?

The colon isn't really a comment; it's a command that doesn't do anything
(however, it returns "true", so "while :" is equivalent to "while true" -
and faster, since ":" is built-in).  The 4.xBSD Bourne shell and the System
<n> Bourne shell also support real comments; a "#" on a command line causes
the rest of the line to be ignored by the shell.

	Guy Harris

lee@eel.UUCP (06/11/85)

The ":" character in the Bourne shell names the null command; it is not a
comment character.  The comment character is "#" (I understand that some
versions of the Bourne shell did not have this implemented). The difference
is that after the "#", no characters are interpreted, whereas after the ":"
all characters are interpreted as they would be for any command (including,
for example, commands in "`" characters so that the side-effects of embedded
commands will occur).

jerryp@tektools.UUCP (Jerry Peek) (06/13/85)

In article <1194@uwmacc.UUCP> jwp@uwmacc.UUCP writes:
> ... the comment
> 
> : to use this file type <name> | lpr
> 
> produces a message referring to the pipe, even though the whole
> line is commented out.  What am I missing?

The problem is that the ":" operator isn't a true comment character.
It's a "do-nothing" command.  The shell still evaluates the rest of the
line.  So, characters like  ' " | >  have an effect.  Be careful about this.

The colon command is useful as a substitute for the "true" command.
For instance, you can use it to make an endless "while" loop...
	
	while :
	do
		command
		command
	done

It works great in an "if" structure, too:

	if something
	then :
	else
		command
		command
	fi

Finally, it's useful with parameter substitution.  The example below checks to
see if the USER variable is set.  If not, the shell complains and exits:

	: ${USER?}

The ":" command is built into the shell.  So, it's faster than using "true",
which is actually a shell script.

--Jerry Peek, UNIX Training Instructor, Tektronix, Inc.
US Mail:    MS 74-222, P.O. Box 500, Beaverton, OR 97077
uucp:       {allegra,decvax,hplabs,ihnp4,ucbvax}!tektronix!tektools!jerryp
CS,ARPAnet: jerryp%tektools@tektronix.csnet
Phone:      503/627-1603

carl@bdaemon.UUCP (carl) (06/13/85)

For example, the comment
> 
> : to use this file type <name> | lpr
> 
> produces a message referring to the pipe, even though the whole
> line is commented out.  What am I missing?

The problem is that the : is a no-op instead of a comment delimiter.
Everything following the colon is evaluated and zero is returned.

Carl Brandauer

psc@lzwi.UUCP (Paul S. R. Chisholm) (06/13/85)

< Smokey the Bar says, "Help stamp out software pirates" [squish!] >

In article <1194@uwmacc.UUCP>, jwp@uwmacc.UUCP writes:
> I've noticed that on my system (2.8BSD) the Bourne shell
> comment character doesn't do what I expected.  The shell
> still looks at some characters on the line, even though
> the line is commented out.  For example, the comment
>	: to use this file type <name> | lpr
> produces a message referring to the pipe, even though the whole
> line is commented out.  What am I missing?

The colon is *NOT* a comment character.  Instead, it's a builtin
command, like cd, which happens to do nothing.  Filenames are
expanded, and quotes must be matched.  (The favorite Unix(tm)
trivia question of a friend of mine, mtgzz!ecl, is, "What does
the following do?"
	: " This is a comment
	nroff -mm lots-o-stuff
	: " Gee, that nroff ran fast!
This is one colon command, whose first argument goes from the first
to the second quote, inclusive.)

More recent versions of the Bourne shell *do* have a comment character:
"A word beginning with # causes that word and all the following
characters up to a new-line to be ignored."
---
Unix is a trademark of some entity withing AT&T, but I'm not sure what
its name is this week.  "AT&T's Bell Laboratories", maybe??
-- 
       -Paul S. R. Chisholm       The above opinions are my own,
       {pegasus,vax135}!lzwi!psc  not necessarily those of any
       {mtgzz,ihnp4}!lznv!psc     telecommunications company.

steven@luke.UUCP (Steven List) (06/13/85)

In article <1194@uwmacc.UUCP> jwp@uwmacc.UUCP writes:
>
>: to use this file type <name> | lpr
>
>produces a message referring to the pipe, even though the whole
>line is commented out.  What am I missing?

You're operating under a misunderstanding.  The colon is not a comment
character!  The comment character is the pound/sharp/octalthorpe (#).
The colon is a builtin COMMAND.  The definition from my manual is:

	No effect; the command does nothing.  A zero exit code
	is returned.

It is, however, evaluated!  I ran into the same thing, thus the ready
answer :->~
-- 
***
*  Steven List @ Benetics Corporation  *  (415) 940-6300
*  {cdp,greipa,idi,oliveb,sun,tolerant}!bene!luke!steven
***

apm@iclbra.UUCP (Andy Merritt) (06/14/85)

> I've noticed that on my system (2.8BSD) the Bourne shell
> comment character doesn't do what I expected.  The shell
> still looks at some characters on the line, even though
> the line is commented out.  For example, the comment
> 
> : to use this file type <name> | lpr
> 
> produces a message referring to the pipe, even though the whole
> line is commented out.  What am I missing?
> 
> -- 
> 	Jeff Percival ...!uwvax!uwmacc!jwp

According to the SysV documentation I have, ':' is a special command for which
the manual says
:	no effect; the command does nothing. A zero exit code is returned.

This implies that the effect is not to ignore the rest of the line as you  
expected, but to take as arguments anything up to a normal terminator and
then do nothing with them. The | is met in this case, and nothing is in the
input for the lpr.

I guess this must be what is happening on the 2.8BSD system also.

		Andy
    /^^^\
   ( o o )
--w---U---w--	  UUCP: ...!ukc!stc!iclbra!apm
"Wot, no news?"   MAIL: Andrew Merritt, ICL, Lovelace Rd, Bracknell, Berkshire

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (06/16/85)

: is not a comment character.  It is a command that does no I/O and
always returns exit status 0.

foss@ihuxe.UUCP (foss) (06/17/85)

> I've noticed that on my system (2.8BSD) the Bourne shell
> comment character doesn't do what I expected.  The shell
> still looks at some characters on the line, even though
> the line is commented out.  For example, the comment
> 
> : to use this file type <name> | lpr
> 
> produces a message referring to the pipe, even though the whole
> line is commented out.  What am I missing?
> 
.................................

The colon (:) is not just a 'comment' character, it is a command
which has no effect.  The message about the pipe is to be expected.
Try using the octothorpe (#) as a comment character.

R. M. Foss