Environment Variables

From DSL Wiki


Using Variables in DSL

What Are Environment Variables?

Your environment in Linux is, to put it generally as it relates to variables, a set of values which determine how applications behave and where they look for certain files. This is a generalization and not to be taken as a solid definition, but this article will focus on the use of variables themselves rather than on semantics.

A variable is simply a name which can be used to represent some form of data. This data could be a command, the location of a file, your username, or any number of other types of information. A Linux system makes very frequent use of variables, whether or not the user is aware of it. They are used in shell scripts and other programs, and are referenced with almost every action. The simple act of opening a shell will reference a number of variables, from the application search path to the user's home directory to the text that is displayed in the prompt.

A variable name can be practically anything, as its value can be. There are a few exceptions, such as beginning a variable name with anything other than a letter, and there are "reserved" variable names (some of them are listed below) which the system uses for specific purposes and should not be used for storing arbitrary data.

Using Variables

When a variable is set, its name and value apply to the shell in which it is set. The syntax of this action in Bash is

The variable name is generally capitalized for aesthetic reasons, although this is not a strict rule. The value can sometimes be used without quotes, but it is safer to be consistent. Any variable value which contains spaces will fail unless you use quotes or escape every space used.

Setting a value to a variable makes that variable available to the currently active shell. When that shell closes the variable goes POOF! into the void. You can make a variable available to child shells (shells that are opened from within the current shell) by using the export command. In Bash this can be accomplished on a single line:

       export VARIABLE_NAME="value"

In some other shells the export command must be used after the variable is set:

       export VARIABLE_NAME

Both of these methods work in Bash shells.

As said, export allows child shells access to variables set by their parent. However, a variable cannot be passed from a child to a parent, or to any other existing shell. Because of this, some commonly used variables are often exported from startup scripts such as .bash_profile or .xinitrc so they will be available to all subsequent shells.

Referencing a variable in Bash requires using the variable name prefixed by a dollar sign ($). A standard method for discovering the current value is to use this command:

       echo $VARIABLE_NAME

Common Linux Variables

Here is a list of some variables which are used regularly by scripts and programs in DSL

HOME The current user's home directory. In DSL this defaults to /home/dsl for user 'dsl'.
USER The current user's username.
UID The current user's user ID. In DSL this is 1001 for user 'dsl' and 0 for user 'root'.
SHELL The current user's shell. In DSL this defaults to Bash (or, more specifically, /bin/bash).
PATH A colon-separated list of directories in which your shell searches, in the specified order, for executeable files.
LD_LIBRARY_PATH A colon-separated list of directories that are seached for program libraries. In DSL this is a useful variable to use with some *.tar.gz and *.uci MyDSL extensions, which are not installed in standard locations.
PWD The current directory.
DISPLAY The ID of the current display. This variable is often used to determine whether or not an X program can run, or to control multiple X sessions.
TERM The terminal type currently being used. In console this is usually "linux", and "xterm" in X. I'm not sure how much use this gets in DSL.

A note about the PATH variable:
This variable is used every time an application is called without a full filename path. Nearly all of the base Linux utilities make use of this variable, so you should exercise caution when setting it. A general practice is to add to this variable rather than to replace it with a different value. This is accomplished by including the PATH variable itself as part of the new value. If you want to add a directory to the end of PATH, the new value should begin with $PATH:, and if you want to add a directory to the beggining of PATH, the new value should end with :$PATH. For example, if you have a directory named /home/dsl/bin which you want to put at the start of PATH, you could do something like this:

       export PATH="$HOME/bin:$PATH"

Notice the $HOME in that command. This demonstrates another use of variables. When Bash reads that command, it will be interpreted as
export PATH="/home/dsl/bin:/usr/local/bin:/usr/bin:/bin:/opt/bin:."
or something similar to that, depending on what the original PATH variable contained.