In many
operating systems, such as
UNIX (and its free incarnation
Linux), as well as
MS-DOS and
Windows, " environment" refers to a set of
variables, always taking
string values, that can be read or written to by a program, but are inherited by all processes the program may spawn. Think of it as a little piece of the
shell that every program carries with itself.
Environment variables are best suited for values that are used once at the beginning of a program, and are ALWAYS set to one value or another. That is, a variable that is *so* frequently used that it becomes noise on a command line should be an environment variable.
Although every program has the potential to access its own environment, and make changes that will be reflected in any
processes it spawns, the program best suited to setting environment variables is the operating system's
shell. Each shell has its own
syntax for setting them.
MS-DOS:
varname=value
Bourne Shell:
varname=value
export value
C Shell:
export varname=value
Any *n?x shell, for the execution of one program:
varname=value program-name program-arguments...
Programs written in
high-level languages (such as
C) for an operating system that uses environments usually include functions for retrieving and setting environment variables. To this end, the C Standard Library contains a
getenv function and a
setenv function. Also, a C program's
main() function can be declared with a third
formal parameter, viz.
int main (int argc, char *argv[], char **envp)
When the program is run, the shell (actually one flavor of
exec()) passes a copy of the current environment into
main. Each string in the list is of the form
varname=value.
Many scripting languages, such as
awk or
Perl, have their own ways of accessing the envorionment. Every
awk script has an
associative array named
ENVIRON. Thus, the expression
ENVIRON[varname]
will return the indicated variable's value, and
ENVIRON[varname]=value
will set a new value.