The shell and other Linux commands need information to work properly. If you type a command that isn't one of that shell's built-in commands, the shell has to locate an executable file (whose name matches the command you type). The shell needs to know which directories to search for those files. Similarly, a text editor, such as vi, needs to know the type of terminal (even if the terminal happens to be a terminal window in GNOME or KDE).
One way to provide this kind of information to a program is through command-line options. However, if you use that approach, you may have to enter many options every time you start a program. UNIX provides an elegant solution through environment variables.
When you log in as a user, you get a set of environment variables that control many aspects of what you see and do on your Linux system. If you want to see your current environment, go ahead and type the following command in a terminal window:
By the way, the printenv command also displays the environment, but env is shorter.
The env command should print a long list of lines. That whole collection of lines is the current environment and each line defines an environment variable. For example, here is a typical line displayed by the env command:
HOSTNAME=localhost.localdomain This line defines the environment variable HOSTNAME as localhost.localdomain.
An environment variable is nothing more than a name associated with a string. On my system, for example, when I log in with the user name naba, the environment variable named PATH is defined as follows:
The string to the right of the equal sign is the value of the PATH environment variable. By convention, the PATH environment variable is a sequence of directory names, each name separated from the preceding one by a colon (:).
When the shell has to search for a file, it simply searches the directories listed in the PATH environment variable. The shell searches the directories in PATH in the order of their appearance. Therefore, if two programs have the same name, the shell executes the one it finds first.
In a fashion similar to the shell's use of the PATH environment variable, an editor such as vi uses the value of the TERM environment variable to figure out how to display the file you are editing with vi. To see the current setting of TERM, type the following command at the shell prompt:
If you type this command in a terminal window, the output is as follows:
To define an environment variable in Bash, use the following syntax:
Here, NAME denotes the name of the environment variable, and Value is the string representing its value. Therefore, you set TERM to the value vt100 by using the following command:
You can set TERM to any of the terminal types defined in the /etc/termcap file, assuming, of course, that the terminal window or physical terminal matches the capabilities of that terminal type.
After you define an environment variable, you can change its value by simply specifying the new value with the syntax NAME=new-value. For example, to change the definition of TERM to ansi, type TERM=ansi at the shell prompt.
With an environment variable such as PATH, you typically want to append a new directory name to the existing definition, rather than define the PATH from scratch. The following example shows how to accomplish this task:
This command appends the string :/sbin to the current definition of the PATH environment variable. The net effect is to add /sbin to the list of directories in PATH.
After you type that command, you can access programs in the /sbin directory such as ifconfig, a program that displays information about the network interfaces.
Note that you also can write this export command as follows:
PATH and TERM are only two of a handful of common environment variables. Table 7-7 lists some of the useful environment variables in Bash.
Table 7-7: Useful Bash Environment Variables
The full path name of the Bash executable program (usually, / bin/bash).
The version number of the Bash program.
The name of the display on which the X Window System displays output (typically set to :0.0).
Your home directory.
The host name of your system.
Your login name.
The location of your mail directory.
The list of directories in which the shell looks for programs.
The shell prompt. (The default is user name and current directory followed by $ for all users except root; for root, the default prompt ends with #.)
Your shell (SHELL=/bin/bash for Bash).
The type of terminal.
Was this article helpful?
This Report Will Show You How To Make Money By Providing Writing Services To Other Internet Marketers. Learn how to make money by writing the right way. Grab your copy of this report now and learn. Why writing is a great way to earn money. How to compete with cheap writers, even if you charge a lot more money.