DistributedNetworks DistributedNetworks


Shell Variables   «Prev  Next»
Lesson 5Creating your own variables
Objective Define new variables to hold information or control a script.

Creating Your own Variables in a Shell Script

You have learned about many types of variables that are provided for you when you execute a shell script. You can also create a variable yourself to hold any information that you need to work with in your script. The variable can have any name that you choose and can contain either text (called string) data or numeric data.
To define a variable in a shell script, you simply refer to a name and assign it a value. After that in the script, you can refer to that name to retrieve or update the value of the variable. For example, if you want to create a variable called AGENTNUMBER with a value of 100, include this line in your script:

AGENTNUMBER=100

Programmers creating shell scripts generally follow a few common-sense rules:
  1. Define all variables that your script uses at the beginning of the script and assign a value to each one. This makes it easy to locate the correct name and initial value of each variable.
  2. Use a standard format for your variable names. It does not matter what it is, but be consistent. Use all capital letters (a common choice), all lowercase, embedded capital letters, embedded underscores, or whatever you choose. If you are consistent, you are less likely to use different names in different locations in the script and wonder why the variable value is set incorrectly.
  3. Use descriptive names for variables so that your shell scripts are easier to understand simply by reading the variable names that you refer to.
Shell scripts do not check how you use variables as many programming languages do. You must be careful about how you set up your variables; small errors in variable naming or use can be difficult to locate in a large script.

Click the link below to hear about how using other scripting tools can really expand what you can do with UNIX.
Unix is case Sensitive
Variables in a shell script are not assigned a data type (string or numeric). You determine the data type by the operations you perform on the variable, as defined later in this module.
Variables can be interpreted in different ways; the next lesson describes how.