User-defined variables allow the user to determine both the name and value of a variable. For example, a user could set a user-defined variable to hold a long path name. Then, instead of typing the long path name everytime it is needed, the variable could be used in its place.

Command Format

VARIABLE=value

unset VARIABLE

Example

In this example we will set a variable to hold the path to the dict directory:

$ PT=/usr/dict
$ echo $PT
/usr/dict

Now we will use the variable to switch to the dict directory. To do this we will use the $ metacharacter in front of the variable. Using the $ in front of a variable name tells the system to use the value of the variable and not the name of the variable.

In the following example, we will switch to the path stored in the PT variable using the cd command and variable substitution:

$ cd $PT
$ pwd
/usr/dict

When you are finished with a variable it is good practice to release the variable with the unset command:

$ unset PT
$ echo $ PT

$

Note: The Bash uses capital letters for shell variable names.