Книга: Fedora™ Unleashed, 2008 edition

Using Environment Variables

Using Environment Variables

A number of in-memory variables are assigned and loaded by default when the user logs in. These variables are known as shell environment variables, which can be used by various commands to get information about your environment, such as the type of system you are running, your home directory, and the shell in use. Environment variables are used by Linux operating systems to help tailor the computing environment of your system, and include helpful specifications and setup, such as default locations of executable files and software libraries. If you begin writing shell scripts, you might use environment variables in your scripts. Until then, you only need to be aware of what environment variables are and do.

The following list includes a number of environment variables, along with descriptions of how the shell uses them:

PWD — To provide the name of the current working directory, used by the pwd command (such as /home/andrew/foo)

USER — To declare the user's name, such as andrew

LANG — To set language defaults, such as English

SHELL — To declare the name and location of the current shell, such as /bin/bash

PATH — To set the default location of executable files, such as /bin, /usr/bin, and so on

LD_LIBRARY_PATH — To declare the location of important software libraries (because most, but not all, Linux commands use shared resources)

TERM — To set the type of terminal in use, such as vt100, which can be important when using screen-oriented programs, such as text editors

MACHINE — To declare system type, system architecture, and so on

NOTE

Each shell can have its own feature set and language syntax, as well as a unique set of default environment variables. See Chapter 15, "Remote Access with SSH," for more information about using the different shells included with Fedora.

At the command line, you can use the env or printenv commands to display these environment variables, like so:

$ env
SSH_AGENT_PID=2881
HOSTNAME=teletran.hudson.com
SHELL=/bin/bash
TERM=xterm
DESKTOP_STARTUP_ID=
USERNAME=andrew
MAIL=/var/spool/mail/andrew
PATH=/usr/kerberos/bin:/usr/lib/ccache:/usr/local/bin:
/usr/bin:/bin:/home/andrew/bin
DESKTOP_SESSION=default
INPUTRC=/etc/inputrc
PWD=/home/andrew
KDEDIRS=/usr
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
SHLVL=2
HOME=/home/andrew
DISPLAY=:0.0

This abbreviated list shows a few common variables. These variables are set by configuration or resource files contained in the /etc, /etc/skel, or user /home directory. You can find default settings for bash, for example, in /etc/profile, /etc/bashrc, .bashrc, or .bash_profile files installed in your home directory. Read the man page for bash for details about using these configuration files.

One of the most important environment variables is $PATH, which defines the location of executable files. For example, if, as a regular user, you try to use a command that is not located in your $PATH (such as the ifconfig command), you will see something like this:

$ ifconfig
-bash: ifconfig: command not found

However, you might know that ifconfig is definitely installed on your system, and you can verify this by using the whereis command, like so:

$ whereis ifconfig
ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz

You can also run the command by typing its full pathname, or complete directory specification like this:

$ /sbin/ifconfig

As you can see in this example, the ifconfig command is indeed installed. What happened is that by default, the /sbin directory is not in your $PATH. One of the reasons for this is that commands under the /sbin directory are normally intended to be run only by root. You can add /sbin to your $PATH by editing the file .bash_profile in your home directory (if you use the bash shell by default, like most Linux users). Look for the following line:

PATH=$PATH:$HOME/bin

You can then edit this file, perhaps using the vi editor (discussed in this chapter), to add the /sbin directory like so:

PATH=$PATH:/sbin:$HOME/bin

Save the file. The next time you log in, the /sbin directory is in your $PATH. One way to use this change right away is to read in the new settings in .bash_profile by using the bash shell's source command as follows:

$ source .bash_profile

You can now run ifconfig without the need to explicitly type its full pathname.

Some Linux commands also use environment variables — for example, to acquire configuration information (such as a communications program looking for a variable such as BAUD_RATE, which might denote a default modem speed).

To experiment with the environment variables, you can modify the PS1 variable to manipulate the appearance of your shell prompt. If you are working with bash, you can use its built-in export command to change the shell prompt. For example, if your default shell prompt looks like

[andrew@teletran ~]$

you can change its appearance by using the PS1 variable like this:

$ PS1='$OSTYPE r001z ->'

After you press Enter, you see the following:

linux-gnu r001z ->

NOTE

See the bash man page for other variables you can use for prompt settings.

Оглавление книги


Генерация: 0.569. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз