Unix Environment Tutorial
There are two types of variables used by the Unix operating system. They are the environment variables and the shell variables. Both types of variables are set by the system, the shell, the user or the applications. Shell variables are short term variables used within the current shell. Environment variables are set when the user logs in to the system and are valid for the length of the session. The environment variables control the user's environment including the default shell, the user's home directory and the path to commands.
-
Variables
-
Convention dictates that environment variables are typed in all upper case letters. This is important because Unix variables are case-sensitive. If you type the variable "SHELL" as "shell," it will not affect the environment SHELL variable.
Environment variables include the USER, HOME, HOST, ARCH, PATH, DESKTOP_SESSION, TERM and SHELL variables. There are many other environment variables. You can see the complete list of variables that are set for your session by typing "printenv" or "env" at a command prompt.
The commands and files that are used to set and unset the variables are different depending on the shell you are using.
C Shell
-
If you are using the C shell, you will use the "printenv," "setenv" and "unsetenv" commands and the ".cshrc" and ".login" files.
The "printenv" command provides the current value of the environment variables. This is a long list, so you can either pipe it through "less" with the command "printenv | less," or send it to a file with the command "printenv > env.txt."
Environment variables can be set for the length of a session using the "setenv" command. For example, the command "setenv SHELL "/bin/bash/"" changes the default shell to the Bourne Again shell (bash) for the current session. If you want to remove an environment variable, you will use the "unsetenv" command. The syntax to remove the PATH variable would be "unset PATH." If you actually remove your PATH variable, you would have to type the complete path to each command to use it during the current session. If you opened another session, your PATH variable would revert to the original value.
If you want the variable to be changed for more than a single session, you must place the variable in the ".login" file in your home directory. The ".login" file is a ASCII text file that is read when the user logs into the machine. To change the default value for your terminal type (TERM variable), type the following line in the ".login" file:
setenv TERM vt220
Bourne Again (Bash) Shell
-
If you are using the bash shell, or the Bourne (sh) shell, you will use the "env," "export," and "unset" commands and the ".profile" file. The "env" command shows the current values of the environment commands. The "export" command changes the value of the variable for the current session. The syntax to change the shell to the C shell is "export SHELL=/bin/csh." The "unset" command is used to unset the variable for the current session. The syntax to unset the PATH variable for the current session is "unset PATH."
To change the variables for more than a single session, you place the new value for the variable in the ".profile" file in your home directory. The ".profile" file is an text file, like the ".login" file. The way you specify the values is a bit different. To change the TERM variable, you would type the following line in the file:
TERM=vt220
-