Title graphic

Substitutions - Part 1

#!/bin/bash

# make_page - A script to produce an HTML file

cat <<- _EOF_
    <HTML>
    <HEAD>
        <TITLE>
        My System Information
        </TITLE>
    </HEAD>

    <BODY>
    <H1>My System Information</H1>
    </BODY>
    </HTML>
_EOF_
       

Now that we have our script working, let's improve it. First off, we'll make some changes because we want to be lazy. In the script above, we see that the phrase "My System Information" is repeated. This is wasted typing (and extra work!) so we improve it like this:

#!/bin/bash

# make_page - A script to produce an HTML file

title="My System Information"

cat <<- _EOF_
    <HTML>
    <HEAD>
        <TITLE>
        $title
        </TITLE>
    </HEAD>

    <BODY>
    <H1>$title</H1>
    </BODY>
    </HTML>
_EOF_
       

As you can see, we added a line to the beginning of the script and replaced the two occurrences of the phrase "My System Information" with $title.

Variables

What we have done is to introduce a very fundamental idea that appears in almost every programming language, variables. Variables are areas of memory that can be used to store information and are referred to by a name. In the case of our script, we created a variable called "title" and placed the phrase "My System Information" into memory. Inside the here script that contains our HTML, we use "$title" to tell the shell to substitute the contents of the variable.

As we shall see, the shell performs various kinds of substitutions as it processes commands. Wildcards are an example. When the shell reads a line containing a wildcard, it expands the meaning of the wildcard and then continues processing the command line. To see this in action, try this:

[me@linuxbox me]$ echo *

Variables are treated in much the same way by the shell. Whenever the shell sees a word that begins with a "$", it tries to find out what was assigned to the variable and substitutes it.

How to create a variable

To create a variable, put a line in your script that contains the name of the variable followed immediately by an equal sign ("="). No spaces are allowed. After the equal sign, assign the information you wish to store. Note that no spaces are allowed on either side of the equal sign.

Where does the variable's name come from?

You make it up. That's right; you get to choose the names for your variables. There are a few rules.

  1. It must start with a letter.
  2. It must not contain embedded spaces. Use underscores instead.
  3. Don't use punctuation marks.
  4. Don't use a name that is already a word understood by bash. These are called reserved words and should not be used as variable names. If you use one of these words, bash will get confused. To see a list of reserved words, use the help command.

How does this increase our laziness?

The addition of the title variable made our life easier in two ways. First, it reduced the amount of typing we had to do. Second and more important, it made our script easier to maintain.

As you write more and more scripts (or do any other kind of programming), you will learn that programs are rarely ever finished. They are modified and improved by their creators and others. After all, that's what open source development is all about. Let's say that you wanted to change the phrase "My System Information" to "Linuxbox System Information." In the previous version of the script, you would have had to change this in two locations. In the new version with the title variable, you only have to change it in one place. Since our script is so small, this might seem like a trivial matter, but as scripts get larger and more complicated, it becomes very important. Take a look at some of the scripts in the Script Library to get a sense of what large scripts look like.

Environment Variables

When you start your shell session, some variables are already ready for your use. They are defined in scripts that run each time a user logs in. To see all the variables that are in your environment, use the printenv command. One variable in your environment contains the host name for your system. We will add this variable to our script like so:

#!/bin/bash

# make_page - A script to produce an HTML file

title="System Information for"

cat <<- _EOF_
    <HTML>
    <HEAD>
        <TITLE>
        $title $HOSTNAME
        </TITLE>
    </HEAD>

    <BODY>
    <H1>$title $HOSTNAME</H1>
    </BODY>
    </HTML>
_EOF_
       

Now our script will always include the name of the machine on which we are running. Note that, by convention, environment variables names are uppercase.


© 2000-2014, William E. Shotts, Jr. Verbatim copying and distribution of this entire article is permitted in any medium, provided this copyright notice is preserved.

Linux® is a registered trademark of Linus Torvalds.