--> --> Variables in Unix -->

Variables in Unix

There are two types of variables:

  1. Shell variables: Shell variables are variables that are used to configure options of the shell as well as allow the user to specify/modify an item or list of items as variables.
  2. Environmental variables: Environmental variables are mainly used to configure external programs by setting different values that they each can view. Only environmental variables are visible to external programs.

There are many built in shell variables that allow you to view modify, turn on and off certain features of the shell. Some variables need not have a value, just the fact that they are set (i.e. the variable exist but with out a value associated with it) triggers a feature it controls to exist. Some of the variables, such as cwd, are there for mostly internal purposes only-they may be given a different value, but it has no significance to the shell and may be overwritten at any time. To view all of the variables and any value associated with them, type set by itself. If you want to view a single variables and it's value (if any) type echo $<variable>, if the variable exists it will print the variable name, and any value that it may have. You use the $ character in front of the variable name to indicate to the shell that the name is actually a variable, however the $ is not part of the variable name.

To set a variable, you use the shell command set followed by the variable name. If you wish to give a variable to a value, follow the variable by an equals sign and the value you wish to set (i.e. set x=mrvalue). If you need to give a variable more than one value, group all the values together using parentheses, or the shell will give the variable the value of the first item: set x=(1 2 3 4). To take a variable off the variable list, use the unset command. This will both remove the value of a variable and remove it from the list of variables.

Some of the more interesting variables are listed here, for a more complete list, check out the shell man page.

How are we doing? Comments on this page?
Office of Information Technology
Office of Information Technology Help Desk Web Site University of Maryland Web Site Office of Information Technology Web Site