Bash Scripting for Ubuntu: Episode 2

This is episode 2 of a serialized article

It was started to explain certain features of the bash shell scripting language. I will be posting it across several weeks as time permits.  please feel free to ask questions and we can answer them.  When the entire article has been serialized, I will send it for free to people who have offered comments.

The topic today is Comments – Who writes them and who needs them.  In this case the comments are not blog comments, which all blog writers prize but comments in the code, which are prized by the programmer who must follow up and add to or edit the code.

Comments are Your Friend

In the Bash shell, any line that begins with a pound ‘#‘ character in the first column is considered a comment and is ignored. The only exception is the first line in the file, where the comment is used to indicate which shell should be used (/bin/sh is the Bash shell). Use comments liberally to explain what a script is for, and what various lines are doing. You can even put a ‘#‘ sign in the middle of a line, but be careful. Everything in the line after the ‘#‘ is going to be ignored by the shell when reading your script.

All lines without a ‘#‘ character at the beginning are taken to be Linux or bash commands and are executed. Any Linux command can be used. For example, the script below displays the current directory name using the pwd command, and then lists the directory contents using the ls command.

#!/bin/sh
# look-around.sh
# print out current directory name and all contents
pwd
ls -al

The comments in this case are only at the top.  Line one is the previously-mentioned reference to where the shell program is in the file system of most Linuxes or Unixes (Unices?).  The second line is the name you have chosen for the file.  It is useful, especially if one is developing several scripts, to have that hint.  When editing a script in vi or nano, the name of the file being edited may not be visible, and this can cause issues, if one forgets which one is being edited.  The third line is a description of what the file does.  if this is your own script, you might not need this prompt, but it is helpful for those who follow you.

Inline commenting must be at the end of the line.  Everything after the # is ignored by the system.  The following line will fail to use grep to filter the results.

dmesg #(to look at hardware, piped to..) | grep nvidia #(to look for nVidia video cards)

If you have any comments about commenting, please comment below.

This is the end of episode 2.  Please stay tuned for another riveting episode where we answer the burning question, “What is an environmental variable, and where do I find them?”

== Wolf Halton is CEO of Atlanta Cloud Technology, Inc. ,  a guest writer on security topics here, at  and owner of Wolfhalton.info.

This entry was posted in Bash, Shell Script. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>