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.
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.
You make it up. That's right, you get to choose
the names for your variables. There are a few
- It must start with a letter.
- It must not contain embedded spaces. Use
- Don't use punctuation symbols.
- 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
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 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.
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 set
command. One variable in your environment contains
the host name for your system. We will add this
variable to our script like so: