Unix Shell Scripts   «Prev  Next»
Lesson 7 Including comments in shell scripts
ObjectiveExplain the importance of comments in a shell script.

Including Comments in Shell Scripts

The two commands you have just learned, echo and read, are straightforward. But as scripts become longer and more complex, the meaning of commands is sometimes difficult to determine. This is especially true when:
  1. You wrote the shell script months (or years) ago and cannot recall exactly how you did things
  2. Someone else wrote a shell script and you need to update it or use it as the basis for another project
  3. You want to improve the appearance of the shell script to make it easier to follow what’s going on in it

When to use Comments

For both of these reasons, comments are added to scripts to explain what each command or section of commands does. Comments are not interpreted by the shell: they are ignored. So adding many comments does not affect the speed of your shell script. Shell script comments generally should not explain the meaning of the commands themselves. You will become very familiar with the commands. Rather, comments should explain why you use a certain command or what it accomplishes in the script overall.

For example, a poor comment would be: Test the value of the variable. If someone reads your script, they might think:
“Yes, I can see that, but why are you testing it?”
A better comment would be:
Test whether flight number is within a valid range.

How to add comments

A comment in a script is any line that begins with a hash mark (also called a pound sign): #. When the shell finds a # character, everything until the end of the line is ignored. In a large shell script, a block of comment lines are often used to introduce a section of the script, something like this:

# Test flight numbers stored in travel records
# to determine validity. Base on the airline
# place records in appropriate database directory
# Created 12 Nov 2009; Modified 20 Jan 2010;
# N. Wells for FarAway.com

The exception to a hash mark being a comment is when the first line of a shell script begins with #! to indicate the path to the interpreter, such as #!/bin/sh. This was described in an earlier lesson in this module.
In addition to using comment statements in scripts, you can make your script self- commenting by using descriptive names for variables.
The following SlideShow shows how a simple script is executed, skipping over comments that describe the function of each section of the script.

1) Shell Script Comment 1 2) Shell Script Comment 2 3) Shell Script Comment 3 4) Shell Script Comment 4 5) Shell Script Comment 5 6) Shell Script Comment 6
  1. The first line of the script names the command interpreter
  2. Blank lines are skipped.
  3. All comment lines are skipped; the first line to be executed is the line with the echo command.
  4. The next line executed is the line containing the read command.
  5. Blank lines and comments are skipped;
  6. The next read command is executed, and the script proceeds with other commands

Shell Script Comments
The next lesson describes how to set a shell script file’s permissions to allow it to be executed.

You can use sed command to comment lines between two line numbers. Example:
sed -i '10,30 s/^/#&/' mycode.sh

Above code will comment the code between line 10&30.