Actions

Assignment::Environment variable

::concepts

Variable::anchor    Command::windows    DR-DOS::under    COMMAND::current    Ordner::schulz    System::returns

Assignment The commands env and set are also used to set environment variables and are often incorporated directly into the shell.

Unix

In Unix, the following commands can also be used, but are often dependent on a certain shell.

VARIABLE=value         #
export VARIABLE        # for Bourne and related shells
export VARIABLE=value  # for ksh, bash, and related shells
setenv VARIABLE value  # for csh and related shells

A few simple principles govern how environment variables achieve their effect.

Environment variables are local to the process in which they were set. If two shell processes are spawned and the value of an environment variable is changed in one, that change will not be seen by the other.

When a child process is created, it inherits all the environment variables and their values from the parent process. Usually, when a program calls another program, it first creates a child process by forking, then the child adjusts the environment as needed and lastly the child replaces itself with the program to be called. This procedure gives the calling program control over the environment of the called program.

In Unix and Unix-like systems, the names of environment variables are case-sensitive.

In Unix shells, variables may be assigned without the export keyword. Variables defined in this way are displayed by the set command, but are not true environment variables, as they are stored only by the shell and not recognized by the kernel. The printenv command will not display them, and child processes do not inherit them.

VARIABLE=value

However, if used in front of a program to run, the variables will be exported to the environment and thus appear as real environment variables to the program:

VARIABLE=value program_name [arguments]

The persistence of an environment variable can be session-wide or system-wide.

DOS, OS/2 and Windows

In DOS, OS/2 and Windows command-line interpreters such as COMMAND.COM and cmd.exe, the SET command is used to assign environment variables and values using the following arguments:

SET VARIABLE=value

The SET command without any arguments displays all environment variables along with their values.


Environment variable sections
Intro   Details    Use and display    Assignment    True environment variables    Pseudo-environment variables   Critics   See also   References   External links   

Assignment
PREVIOUS: IntroNEXT: Details
<<>>