Environment variable

As an environment variable is called configurable variables in operating systems, which often contain paths to certain programs or data, as well as certain data and settings that can be used by several programs. In general, there are strings.

Another name is also a global variable; However, this is rather unusual, as it is used in many programming languages ​​in a different meaning.

Users or applications can read values ​​of these variables and / or change.

  • 2.1 Set
  • 2.2 query
  • 2.3 Put out from programs
  • 2.4 Windows command line
  • 2.5 Graphical Interface
  • 2.6 User Variables and System Variables
  • 2.7 Dynamic Environment Variables
  • 2.8 Predefined Environment Variables
  • 2.9 Registration
  • 2:10 delay in the application of modified variables

Unix and Unix -like operating systems

Command line

In many UNIX-based operating systems, such as OS X, Linux or BSD, environment variables are set at the start of a command line interpreter ( shell). Pre-defined environment variables are set automatically when you start a shell according to the entries in one or more configuration files in general. In the Bourne shell, this is for example the file / etc / profile. In addition, the shell uses an existing in the home directory of the user file (eg. Profile or. Cshrc ) that contains user-specific environment variables and can be customized by the user. These pre-defined environment variables can be used to output by typing printenv in the shell also. The env program can run a program in a modified environment. Both commands are part of the coreutils.

Predefined Environment Variables

Some environment variables can be found again on almost all Unix systems. Examples are:

Change of environment variables

Environment variables can be set as follows and are made available to other processes known within the operating system:

In Bourne, Bash, and building upon the Shells:

In csh, tcsh, and building upon the Shells:

Display the environment variable

In all shells:

Process dependence

It is common that the environment variables are stored per system process. Changes made by a process to the variables that are valid only for himself and for processes that were started by him ( child processes ).

Microsoft

The functionality under MS DOS and the Windows shell or command line is basically the same. On Windows, more features and default variables were added.

Set

Environment variables can be set from the command line or from batch files (. Cmd, . BAT) with the SET command (for example, SET PROMPT = $ P $ G). If the SET command, all environment variables are specified alone displayed, and only the name of a variable is specified as the value is displayed. Can you delete it by the variable name followed by = is specified (for example, SET PROMPT =).

Query

In Stapelverabeitungsdateien (. BAT) environment variables can be used by being bracketed in percent signs (eg CD % VARIABLE %). The called program then receives the command line with substituted variable.

Furthermore, each program receives a copy of the environment variables of the calling program (for normal call to the COMMAND.COM ) in his surrounding segment ( what a pointer in the PSP to address 2Ch references ). The environment of a program segment is discarded after its termination.

Put out from programs

The mechanism of the environment variable does not anticipate that from a program or a nested invoked shell ( cmd ) environment out the environment variables of the parent shell can be changed. Due to the inheritance of variables to changes in their own environment do not affect the environment of COMMAND.COM (Master Environment). A call to the command line command SET from a program has no effect because this starts a new instance of COMMAND.COM.

Under DOS and older Windows versions The following trick was possible: Area of COMMAND.COM could be tuned by 52h at address [ BX -2h ] relating the first memory control block via INT 21 AH =, and then searches memory control block for memory control block, until you find the area of COMMAND.COM and changed this. However, this does not work from Windows NT, since there the vicinity of COMMAND.COM is regularly re-loaded from the NT kernel.

The definition of an environment variable in a program and use the following in a shell or batch scripts is possible by the shell or batch script is started from this program out in the program after changing their own environment. Such a call is usually supported in programming languages ​​by calling the respective operating system feature, in Java, for example, with the java.lang.ProcessBuilder class. The changed environment of the program then acts as a parent for this call.

In Windows batch programs ( under DOS ) affects the change of the environment of a called batch (call command) to the calling level, because the COMMAND environment will not leave it. This is not to call a Unix shell script from within a script. The called script is embedded in a new (child ) environment, which therefore does not apply to the calling level.

In Windows, it is now possible to influence from within a program environment yet: The program must create a small batch file in a temp directory or the current directory with the following content:

Set VARIABLE = VALUE This temporary batch file is then called in the overall batch below:

To display the value of an environment variable in the command line, use echo% NAME% or set NAME, being used for NAME of the variable name. The set command is also a command-line editor. It can also be very well used within batch files. The command set such as lists all set and thus available environment variables. If you want to create an environment variable or assign a new value to an existing, uses one set NAME = VALUE where NAME instead of the name and place VALUE of the future value of the variable will be used. set /? provides detailed information about the functionality of the command.

Graphical interface

A graphical editor for direct editing of the environment variable provides the command rundll32 sysdm.cpl, EditEnvironmentVariables or the menu item " Control Panel - System " or the key combination Windows Pause. The editor is under the "Advanced" tab and find under " Environment Variables".

User Variables and System Variables

Newer versions of Windows (such as Windows XP or Windows 7) distinguish between " user variables ", which refer only to the currently logged-in user, and "System variables " which (also all other user accounts ) are valid for the entire system.

Dynamic environment variables

Some DOS and Windows programs themselves already have dynamically generated environment variables. These are not permanently stored, and the value will be determined shortly before the output. Examples of such a dynamic environment variables are:

Predefined Environment Variables

Depending on the Windows version are further defined at system startup or during login environment variables available. The values ​​may depend on the language and of the drive letter for the operating system drive. For example:

Registration

Among the newer NT -based versions of Windows (Windows NT/2000/XP/2003/Vista ) are stored in the registry, environment variables. The registry path

HKEY_CURRENT_USER \ Environment or for Windows XP ( for eg HOMEDRIVE ) and Windows Vista

HKEY_CURRENT_USER \ Volatile Environment is used for environment variables that affect only the current user. The path

HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Environment on the other hand is used to store environment variables that are valid in the entire system. Using the search function of Registry Editor can be the key paths or on the desired entries to search.

With the command regedit in the Run dialog (Windows R ) can be started, the editor.

Caution: Improper changes to the registry can have serious consequences. Changes should only be made if you know what you're doing. It should be made sure to back up the relevant registry before any changes!

Delay in the application of modified variables

At the start of a process ( exe file ), the current environment variables are read from the registry and then "frozen", so the process gets to his emergence a copy or " snapshot " of the current variable. Later changed environment variables therefore do not affect the running processes from (as long as the program is the Windows message ignored ) so that processes such as a runny Shell ( "Prompt" ) heard nothing of it. If the environment variables are used in a shell that needs to be loaded, a new instance of the shell. It does not matter if an already open shell was closed with the old environment variables before or not. It can be accomplished two shells with different environment variables.

If the shell is started from a Windows explorer activated (eg, via " Open Command Window Here" ), and the option " folder windows in a separate process start " in the settings of Windows Explorer, so the windows must Explorer will be restarted for the new settings are visible.

758542
de