Comment lines in the Bourne shell

Jerry Peek jerryp at tektools.UUCP
Thu Jun 13 13:24:55 AEST 1985


In article <1194 at uwmacc.UUCP> jwp at 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 at tektronix.csnet
Phone:      503/627-1603



More information about the Comp.unix mailing list