In many operating system
s, such as UNIX
(and its free incarnation Linux
), as well as MS-DOS
, " environment" refers to a set of variable
s, 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 process
es 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.
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
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
, have their own ways of accessing the envorionment. Every awk
script has an associative array
. Thus, the expression
will return the indicated variable's value, and
will set a new value.