The Linux Command Line---Variables

The Linux Command Line by William Shotts



#!/bin/bash

# sysinfo_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

# sysinfo_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 perform parameter expansion and replace the name of the variable with the variable's contents.
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.

Where Do Variable Names Come From?

You make them up. That's right; you get to choose the names for your variables. There are a few rules.
  1. Names must start with a letter.
  2. A name must not contain embedded spaces. Use underscores instead.
  3. You cannot use punctuation marks.

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 importantly, 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 titlevariable, 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.

Environment Variables

When you start your shell session, some variables are already set by the startup file we looked at earlier. 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

# sysinfo_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.