Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Environment variable
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Syntax == The variables can be used both in scripts and on the [[command line]]. They are usually referenced by putting special symbols in front of or around the variable name. By convention, names of environment variables are normally expressed in all capital letters. This helps keep environment variables distinctly different from other variables and identifiers used in programming codes. Nevertheless, note that case sensitivity in environment variable names differs between operating systems. That is, Unix-like operating systems are case-sensitive with respect to environment variable names, while DOS, OS/2, and Windows are not case-sensitive. === Unix === In most Unix and Unix-like [[command-line shell]]s, an environment variable's value is retrieved by placing a <code>$</code> sign before the variable's name. If necessary, the name can also be surrounded by braces. To display the user home directory, the user may type: <syntaxhighlight lang="bash"> echo $HOME </syntaxhighlight> In Unix and Unix-like systems, the names of environment variables are case-sensitive. The command '''<code>[[env (shell)|env]]</code>''' displays all environment variables and their values. The command '''<code>printenv</code>''' can also be used to print a single variable by giving that variable name as the sole argument to the command. === DOS, OS/2 and Windows === In DOS, OS/2 and Windows command-line interpreters such as <code>COMMAND.COM</code> and <code>CMD.EXE</code>, an environment variable is retrieved by placing a <code>%</code> sign before and after it. In DOS, OS/2 and Windows command-line interpreters as well as their [[API]], upper or lower case is not distinguished for environment variable names. The environment variable named <code>HOMEDRIVE</code> contains the drive letter (plus its trailing <code>:</code> colon) of the user's home directory, whilst <code>HOMEPATH</code> contains the full path of the user's home directory within that drive. So to see the home drive and path, the user may type this: <syntaxhighlight lang="batch"> ECHO %HOMEDRIVE%%HOMEPATH% </syntaxhighlight> The command '''<code>SET</code>''' (with no arguments) displays all environment variables and their values. In [[Windows NT]] and later <code>set</code> can also be used to print all variables whose name begins with a given prefix by giving the prefix as the sole argument to the command. In [[Windows PowerShell]], the user may type any of the following: <syntaxhighlight lang="powershell"> echo $env:homedrive$env:homepath Write-Output $env:homedrive$env:homepath "$env:homedrive$env:homepath" </syntaxhighlight> In PowerShell, upper or lower case is not distinguished for environment variable names. The following command displays all environment variables and their values: <syntaxhighlight lang="powershell"> Get-ChildItem env: </syntaxhighlight> === Assignment: Unix === The commands <code>env</code> and <code>set</code> can be used to set environment variables and are often incorporated directly into the shell. The following commands can also be used, but are often dependent on a certain shell. ''VARIABLE''=''value'' # (there must be no spaces around the equals sign) export ''VARIABLE'' # for [[Bourne shell|Bourne]] and related shells export ''VARIABLE''=''value'' # for [[KornShell|ksh]], [[Bash (Unix shell)|bash]], and related shells setenv ''VARIABLE'' ''value'' # for [[C shell|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 [[fork (system call)|fork]]ing, then the child adjusts the environment as needed and lastly the child [[exec (system call)|replaces]] itself with the program to be called. This procedure gives the calling program control over the environment of the called program. In Unix shells, variables may be assigned without the '''<code>export</code>''' keyword. Variables defined in this way are displayed by the '''<code>set</code>''' command, but are ''not'' true environment variables, as they are stored only by the shell and are unknown to all other processes. The <code>printenv</code> command will not display them, and child processes do not inherit them. ''VARIABLE''=''value'' The prefix syntax exports a "true" environment variable to a child process without affecting the current process:<ref name="ClassicShS"/> ''VARIABLE''=''value'' program_name [arguments] The persistence of an environment variable can be session-wide or system-wide. '''<code>unset</code>''' is a builtin command implemented by both the [[Bourne shell]] family (<code>sh</code>, <code>ksh</code>, <code>bash</code>, etc.) and the [[C shell]] family (csh, tcsh, etc.) of [[Unix shell|Unix command line shell]]s. It unsets a shell variable, removing it from memory and the shell's exported environment. It is implemented as a [[shell builtin]], because it directly manipulates the internals of the shell.<ref name="OG_unset"/><ref name="Bash_unset"/> Read-only shell variables cannot be unset. If one tries to unset a read-only variable, the <code>unset</code> command will print an error message and return a non-zero exit code. === Assignment: DOS, OS/2 and Windows === In DOS, OS/2 and Windows command-line interpreters such as <code>COMMAND.COM</code> and <code>CMD.EXE</code>, the '''<code>[[SET (DOS command)|SET]]</code>''' command is used to assign environment variables and values using the following arguments: <syntaxhighlight lang="batch"> SET VARIABLE=value </syntaxhighlight> An environment variable is removed via: <syntaxhighlight lang="batch"> SET VARIABLE= </syntaxhighlight> The '''<code>SET</code>''' command without any arguments displays all environment variables along with their values; '''<code>SET " "</code>''', zero or more spaces, will include internal variables too. In <code>CMD.EXE</code>, it is possible to assign local variables that will not be global using the '''<code>SETLOCAL</code>''' command and '''<code>ENDLOCAL</code>''' to restore the environment. Use the [[switch (command line)|switch]] '''<code>/?</code>''' to display the internal [[man page|documentation]], or use the [[help (command)#DOS|viewer]] '''<code>help</code>''': <syntaxhighlight lang="batch"> SET /? HELP SET SETLOCAL /? HELP SETLOCAL </syntaxhighlight> In [[PowerShell]], the assignment follows a syntax similar to Unix: <syntaxhighlight lang="powershell"> $env:VARIABLE = "VALUE" </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)