Environment variables: Difference between revisions

 
(16 intermediate revisions by 13 users not shown)
Line 17:
 
=={{header|11l}}==
<syntaxhighlight lang ="11l">print(os:getenv(‘HOME’))</langsyntaxhighlight>
 
=={{header|Ada}}==
Print a single environment variable.
<langsyntaxhighlight lang="ada">with Ada.Environment_Variables; use Ada.Environment_Variables;
with Ada.Text_Io; use Ada.Text_Io;
 
Line 27:
begin
Put_Line("Path : " & Value("PATH"));
end Print_Path;</langsyntaxhighlight>
Print all environment variable names and values.
<langsyntaxhighlight lang="ada">with Ada.Environment_Variables; use Ada.Environment_Variables;
with Ada.Text_Io; use Ada.Text_Io;
 
Line 39:
begin
Iterate(Print_Vars'access);
end Env_Vars;</langsyntaxhighlight>
 
 
Line 46:
Uses [http://forge.ada-ru.org/matreshka Matreshka].
 
<langsyntaxhighlight Adalang="ada">with Ada.Wide_Wide_Text_IO;
 
with League.Application;
Line 60:
Ada.Wide_Wide_Text_IO.Put_Line
(League.Application.Environment.Value (+"HOME").To_Wide_Wide_String);
end Main;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386 - ''getenv'' is not part of the standard's prelude}}
<langsyntaxhighlight lang="algol68">print((getenv("HOME"), new line))</langsyntaxhighlight>
 
=={{header|APL}}==
<syntaxhighlight lang="apl">
⎕ENV 'HOME'
HOME /home/russtopia
</syntaxhighlight>
 
=={{header|AppleScript}}==
===Invoking Finder===
<lang AppleScript>
<syntaxhighlight lang="applescript">
tell application "Finder" to get name of home
</syntaxhighlight>
</lang>
===Invoking Terminal===
<syntaxhighlight lang="applescript">
"HOME : " & (do shell script "echo $HOME" & ", PATH : " & (do shell script "echo $PATH" & ", USER : " & (do shell script "echo $USER")))
</syntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">print ["path:" env\PATH]
print ["user:" env\USER]
print ["home:" env\HOME]</langsyntaxhighlight>
 
{{out}}
Line 84 ⟶ 95:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">EnvGet, OutputVar, Path
MsgBox, %OutputVar%</langsyntaxhighlight>
 
=={{header|AutoIt}}==
<langsyntaxhighlight AutoItlang="autoit">ConsoleWrite("# Environment:" & @CRLF)
 
Local $sEnvVar = EnvGet("LANG")
Line 98 ⟶ 109:
Func ShowEnv($N)
ConsoleWrite( StringFormat("%-12s : %s\n", $N, EnvGet($N)) )
EndFunc ;==>ShowEnv</langsyntaxhighlight>
 
{{Out}}
Line 108 ⟶ 119:
=={{header|AWK}}==
The ENVIRON array contains the values of the current environment:
<langsyntaxhighlight lang="awk">$ awk 'BEGIN{print "HOME:"ENVIRON["HOME"],"USER:"ENVIRON["USER"]}' </langsyntaxhighlight>
{{out}}
<pre>
Line 114 ⟶ 125:
 
Environment variables can also be assigned to awk variables before execution, with (-v) options:
<langsyntaxhighlight lang="awk">$ awk -v h=$HOME -v u=$USER 'BEGIN{print "HOME:"h,"USER:"u}' </langsyntaxhighlight>
{{out}}
<pre>HOME:/home/suchrich USER:SuchRich</pre>
 
Listing all the environment variables:
<langsyntaxhighlight lang="awk"># http://ideone.com/St5SHF
BEGIN { print "# Environment:"
for (e in ENVIRON) { printf( "%10s = %s\n", e, ENVIRON[e] ) }
}
END { print "# Done." } </langsyntaxhighlight>
{{out}}
<pre>
Line 140 ⟶ 151:
 
=={{header|BASIC}}==
<langsyntaxhighlight lang="qbasic">x$ = ENVIRON$("path")
PRINT x$</langsyntaxhighlight>
 
==={{header|BaCon}}===
''Case matters and needs to match''
<langsyntaxhighlight lang="freebasic">PRINT GETENVIRON$("PATH")</langsyntaxhighlight>
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 ASK BORDER VAR01
110 ASK DEFAULT CHANNEL VAR02
120 ASK EDITOR BUFFER VAR03
Line 175 ⟶ 186:
350 ASK VIDEO MODE VAR26
360 ASK VIDEO X VAR27
370 ASK VIDEO Y VAR28</langsyntaxhighlight>
 
or
 
<syntaxhighlight lang IS="is-BASICbasic">ASK machine-option-code var</langsyntaxhighlight>
 
==={{header|ZX Spectrum Basic}}===
The ZX Spectrum does not use environmental variables in a traditional sense. However, it does provide a set of system variables held at a fixed memory address:
<langsyntaxhighlight lang="zxbasic">10 PRINT "The border colour is "; PEEK (23624): REM bordcr
20 PRINT "The ramtop address is "; PEEK (23730) + 256 * PEEK (23731): REM ramtop
30 POKE 23609,50: REM set keyboard pip to 50</langsyntaxhighlight>
 
=={{header|Batch File}}==
Batch files don't have any other kind of variables except environment variables. They can be accessed by enclosing the variable name in percent signs:
<syntaxhighlight lang ="dos">echo %Foo%</langsyntaxhighlight>
For interactive use one can use <code>set</code> to view all environment variables or all variables starting with a certain string:
<langsyntaxhighlight lang="dos">set
set Foo</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> PRINT FNenvironment("PATH")
PRINT FNenvironment("USERNAME")
END
Line 205 ⟶ 216:
DIM buffer% LOCAL size%
SYS "GetEnvironmentVariable", envar$, buffer%, size%+1
= $$buffer%</langsyntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
Line 217 ⟶ 228:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
 
namespace RosettaCode {
Line 229 ⟶ 240:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <cstdlib>
#include <cstdio>
 
Line 239 ⟶ 250:
puts(getenv("HOME"));
return 0;
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="lisp">(System/getenv "HOME")</langsyntaxhighlight>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. Environment-Vars.
 
Line 262 ⟶ 273:
 
GOBACK
.</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
{{works with|node.js}}
<langsyntaxhighlight lang="coffeescript">for var_name in ['PATH', 'HOME', 'LANG', 'USER']
console.log var_name, process.env[var_name]</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
Access to environment variables isn't a part of the Common Lisp standard, but most implementations provide some way to do it.
{{works with|LispWorks}}
<langsyntaxhighlight lang="lisp">(lispworks:environment-variable "USER")</langsyntaxhighlight>
{{works with|SBCL}}
<langsyntaxhighlight lang="lisp">(sb-ext:posix-getenv "USER")</langsyntaxhighlight>
{{works with|Clozure CL}}
<langsyntaxhighlight lang="lisp">(ccl:getenv "USER")</langsyntaxhighlight>
{{works with|CLISP}}
<langsyntaxhighlight lang="lisp">(getenv "HOME")</langsyntaxhighlight>
Ways to do this in some other implementations are listed in the [http://cl-cookbook.sourceforge.net/os.html#env Common Lisp Cookbook].
 
=={{header|D}}==
{{libheader|phobos}}
<langsyntaxhighlight lang="d">import std.stdio, std.process;
 
void main() {
auto home = getenv("HOME");
}</langsyntaxhighlight>
{{libheader|tango}}
<langsyntaxhighlight lang="d">import tango.sys.Environment;
 
void main() {
auto home = Environment("HOME");
}</langsyntaxhighlight>
 
=={{header|Delphi}}/{{header|Pascal}}==
<langsyntaxhighlight Delphilang="delphi">program EnvironmentVariable;
 
{$APPTYPE CONSOLE}
Line 304 ⟶ 315:
begin
WriteLn('Temp = ' + GetEnvironmentVariable('TEMP'));
end.</langsyntaxhighlight>
 
=={{header|E}}==
{{works with|E-on-Java}}
<langsyntaxhighlight lang="e"><unsafe:java.lang.System>.getenv("HOME")</langsyntaxhighlight>
 
=={{header|Eiffel}}==
The feature <code lang="eiffel">get</code> returns the value of an environment variable. <code lang="eiffel">get</code> is defined in the library class EXECUTION_ENVIRONMENT. So the class APPLICATION inherits from EXECUTION_ENVIRONMENT in order to make <code lang="eiffel">get</code> available.
<langsyntaxhighlight lang="eiffel ">class
APPLICATION
inherit
Line 324 ⟶ 335:
print (get ("USERNAME"))
end
end</langsyntaxhighlight>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">System.get_env("PATH")</langsyntaxhighlight>
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight lang="lisp">(getenv "HOME")</langsyntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
fun showVariable = <text variable|writeLine(variable + ": '" + Runtime.get(variable) + "'")
showVariable("SystemDrive")
showVariable("USERNAME")
# we can get the environment variables as a map
Map variables = Runtime.variables()
writeLine(variables["TEMP"])
</syntaxhighlight>
{{Out}}
<pre>
SystemDrive: 'C:'
USERNAME: 'XXXYYY'
C:\Users\xxxyyy\AppData\Local\Temp
</pre>
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
os:getenv( "HOME" ).
</syntaxhighlight>
</lang>
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">puts(1,getenv("PATH"))</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">open System
 
[<EntryPoint>]
let main args =
printfn "%A" (Environment.GetEnvironmentVariable("PATH"))
0</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">"HOME" os-env print</langsyntaxhighlight>
 
=={{header|Forth}}==
{{works with|GNU Forth}}
<langsyntaxhighlight lang="forth">s" HOME" getenv type</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|any Fortran compiler}}
<langsyntaxhighlight lang="fortran">program show_home
implicit none
character(len=32) :: home_val ! The string value of the variable HOME
Line 371 ⟶ 398:
write(*,'(a)') 'No HOME to go to!'
end if
end program show_home</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Var v = Environ("SystemRoot")
Print v
Sleep</langsyntaxhighlight>
 
{{out}}
Line 387 ⟶ 414:
=={{header|Frink}}==
 
<langsyntaxhighlight lang="funl">callJava["java.lang.System", "getenv", ["HOME"]]</langsyntaxhighlight>
 
{{out}}
Line 395 ⟶ 422:
 
=={{header|FunL}}==
<langsyntaxhighlight lang="funl">println( System.getenv('PATH') )
println( $home )
println( $user )</langsyntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
NSLog(@"%@",fn NSUserName)
NSLog(@"%@",fn NSFullUserName)
NSLog(@"%@",fn NSHomeDirectory)
NSLog(@"%@",fn NSTemporaryDirectory)
 
HandleEvents
</syntaxhighlight>
 
=={{header|Go}}==
;Simply:
<langsyntaxhighlight lang="go">package main
 
import (
Line 410 ⟶ 449:
func main() {
fmt.Println(os.Getenv("SHELL"))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 419 ⟶ 458:
You're on your own then to parse out the one you want.
Example:
<langsyntaxhighlight lang="go">package main
 
import (
Line 437 ⟶ 476:
}
fmt.Println(s, "not found")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 445 ⟶ 484:
=={{header|Gri}}==
Command <code>get env</code> fetches an environment variable into a synonym (a string)
<langsyntaxhighlight Grilang="gri">get env \foo HOME
show "\foo"</langsyntaxhighlight>
 
Quotes can be used in the usual way if the environment variable name contains spaces (which is unusual, but possible).
<langsyntaxhighlight Grilang="gri">get env \foo "X Y Z"</langsyntaxhighlight>
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">System.getenv().each { property, value -> println "$property = $value"}</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import System.Environment
main = do getEnv "HOME" >>= print -- get env var
getEnvironment >>= print -- get the entire environment as a list of (key, value) pairs</langsyntaxhighlight>
 
=={{header|hexiscript}}==
<langsyntaxhighlight lang="hexiscript">println env "HOME"
println env "PATH"
println env "USER"</langsyntaxhighlight>
 
=={{header|HicEst}}==
<langsyntaxhighlight HicEstlang="hicest">CHARACTER string*255
 
string = "PATH="
SYSTEM(GEteNV = string)</langsyntaxhighlight>
 
=={{header|i}}==
<langsyntaxhighlight lang="i">software {
print(load("$HOME"))
print(load("$USER"))
print(load("$PATH"))
}</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
{{works with|Unicon}}
<langsyntaxhighlight Iconlang="icon">procedure main(arglist)
 
if *envars = 0 then envars := ["HOME", "TRACE", "BLKSIZE","STRSIZE","COEXPSIZE","MSTKSIZE", "IPATH","LPATH","NOERRBUF"]
Line 485 ⟶ 524:
every v := !sort(envars) do
write(v," = ",image(getenv(v))|"* not set *")
end</langsyntaxhighlight>
 
=={{header|J}}==
<syntaxhighlight lang ="j">2!:5'HOME'</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">System.getenv("HOME") // get env var
System.getenv() // get the entire environment as a Map of keys to values</langsyntaxhighlight>
 
=={{header|JavaScript}}==
The JavaScript language has no facilities to access the computer: it relies on the host environment to provide it.
{{works with|JScript}}
<langsyntaxhighlight lang="javascript">var shell = new ActiveXObject("WScript.Shell");
var env = shell.Environment("PROCESS");
WScript.echo('SYSTEMROOT=' + env.item('SYSTEMROOT'));</langsyntaxhighlight>
 
=={{header|Joy}}==
<langsyntaxhighlight lang="joy">"HOME" getenv.</langsyntaxhighlight>
 
=={{header|jq}}==
<syntaxhighlight lang ="jq">env.HOME</langsyntaxhighlight>If the environment variable name has spaces or special characters, the name must be given as a string, e.g. <tt>env."HOME"</tt>.
 
=={{header|jsish}}==
The Jsi ''Util'' module provides access to set ''Util.setenv(name, value)'' and get ''Util.getenv(name)'' process environment variables. ''Util.getenv()'', with no argument will return an object with all available name:value pairs.
<langsyntaxhighlight lang="javascript">/* Environment variables, in Jsi */
puts(Util.getenv("HOME"));
var environment = Util.getenv();
puts(environment.PATH);</langsyntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">@show ENV["PATH"]
@show ENV["HOME"]
@show ENV["USER"]</langsyntaxhighlight>
 
=={{header|K}}==
<langsyntaxhighlight Klang="k">_getenv "HOME"</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
// tested on Windows 10
Line 531 ⟶ 570:
fun main(args: Array<String>) {
println(System.getenv("SystemRoot"))
}</langsyntaxhighlight>
 
{{out}}
Line 539 ⟶ 578:
 
=={{header|langur}}==
<langsyntaxhighlight lang="langur">writeln "HOME: ", _env["HOME"]
writeln "PATH: ", _env["PATH"]
writeln "USER: ", _env["USER"]</langsyntaxhighlight>
 
{{works with|langur|0.9}}
We could also the short-hand form of indexing by string. This is limited to code points used for tokens and does not allow for spaces, nor an index alternate.
<langsyntaxhighlight lang="langur">writeln "HOME: ", _env'HOME
writeln "PATH: ", _env'PATH
writeln "USER: ", _env'USER</langsyntaxhighlight>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">#!/usr/bin/lasso9
 
define getenv(sysvar::string) => {
Line 564 ⟶ 602:
stdoutnl(getenv('PATH'))
stdoutnl(getenv('USER'))
stdoutnl(getenv('WHAT'))</langsyntaxhighlight>
{{out}}
<pre>/Users/rosetta
Line 573 ⟶ 611:
=={{header|Liberty BASIC}}==
=== Built-in variables ===
<langsyntaxhighlight lang="lb">print StartupDir$
print DefaultDir$</langsyntaxhighlight>
=== Other variables ===
<langsyntaxhighlight lang="lb">print GetEnvironmentVariable$("USERNAME")
print GetEnvironmentVariable$("USERPROFILE") ' equivalent to UNIX HOME variable
print GetEnvironmentVariable$("PATH")
Line 604 ⟶ 642:
GetEnvironmentVariable$ = left$(lpBuffer$, result)
end select
end function</langsyntaxhighlight>
 
=={{header|LIL}}==
LIL does not ship with a command to retrieve process environment variables. The '''system''' command could be used, but here is an extension in C for the lil shell.
 
<syntaxhighlight lang="c">
<lang c>
static LILCALLBACK lil_value_t fnc_env(lil_t lil, size_t argc, lil_value_t* argv)
{
if (!argc) return NULL;
return lil_alloc_string(getenv(lil_to_string(argv[0])));
}</langsyntaxhighlight>
 
Then inside the main functions for repl and nonint (Interactive, Noninteractive):
<langsyntaxhighlight lang="c">lil_register(lil, "env", fnc_env);</langsyntaxhighlight>
 
Now lil can get at the environment. That could fairly easily be extended further to return the entire environment array if no arguments are passed to '''env''', this just returns an empty result for that case. Defaults values could also be supported if the named environment variable is empty. Etcetera. Setting variables would be similar, a few lines of lil C to wrap a call to libc setenv in a new command, and registering the command.
Line 633 ⟶ 671:
=={{header|Lingo}}==
{{libheader|Shell Xtra}}
<langsyntaxhighlight lang="lingo">sx = xtra("Shell").new()
if the platform contains "win" then
path = sx.shell_cmd("echo %PATH%").line[1]
else
path = sx.shell_cmd("echo $PATH").line[1]
end if</langsyntaxhighlight>
 
=={{header|Logtalk}}==
Using the standard library:
<langsyntaxhighlight lang="logtalk">os::environment_variable('PATH', Path).</langsyntaxhighlight>
 
=={{header|LSL}}==
Rez a box on the ground, and add the following as a New Script.
<langsyntaxhighlight LSLlang="lsl">default {
state_entry() {
llOwnerSay("llGetTimestamp()="+(string)llGetTimestamp());
Line 653 ⟶ 691:
llOwnerSay("llGetMemoryLimit()="+(string)llGetMemoryLimit());
}
}</langsyntaxhighlight>
{{out}}
<pre>llGetTimestamp()=2012-07-18T01:26:12.133137Z
Line 661 ⟶ 699:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">print( os.getenv( "PATH" ) )</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
\\ using read only variablles
Line 691 ⟶ 729:
}
Checkit
</syntaxhighlight>
</lang>
 
=={{header|Make}}==
Make variables are initialized from the environment, so simply
<langsyntaxhighlight Makelang="make">TARGET = $(HOME)/some/thing.txt
foo:
echo $(TARGET)</langsyntaxhighlight>
 
The shell code in a rule can use the shell's environment in the usual way ([[Environment variables#Unix Shell|Unix Shell]]), but remember <code>$</code> must be doubled <code>$$</code> to get a literal <code>$</code> in that code.
<langsyntaxhighlight Makelang="make">bar:
echo "$$HOME"</langsyntaxhighlight>
 
If you mistakenly write just <code>$HOME</code> then it means the makefile <code>$H</code> followed by characters <code>OME</code>.
 
<langsyntaxhighlight Makelang="make">H = oops ...
bar:
echo $HOME
 
# prints oops ... OME</langsyntaxhighlight>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">getenv("PATH");</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Environment["PATH"]</langsyntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight MATLABlang="matlab"> getenv('HOME')
getenv('PATH')
getenv('USER')</langsyntaxhighlight>
 
=={{header|Mercury}}==
<langsyntaxhighlight lang="mercury">:- module env_var.
:- interface.
 
Line 740 ⟶ 778:
MaybeValue = no,
io.write_string("environment variable HOME not set\n", !IO)
).</langsyntaxhighlight>
 
=={{header|min}}==
{{works with|min|0.19.3}}
<syntaxhighlight lang ="min">$PATH</langsyntaxhighlight>
 
=={{header|Modula-3}}==
<langsyntaxhighlight lang="modula3">MODULE EnvVars EXPORTS Main;
 
IMPORT IO, Env;
Line 761 ⟶ 799:
IO.Put(k & " = " & v & "\n")
END
END EnvVars.</langsyntaxhighlight>
 
=={{header|MUMPS}}==
Line 767 ⟶ 805:
 
In Caché on OpenVMS in an FILES-11 filesystem ODS-5 mode these could work:
<langsyntaxhighlight MUMPSlang="mumps"> Set X=$ZF(-1,"show logical")
Set X=$ZF(-1,"show symbol")</langsyntaxhighlight>
 
=={{header|NetRexx}}==
When NetRexx runs under a JVM, system ENVIRONMENT variables are complimented by JVM system properties. This sample shows how to get both.
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 832 ⟶ 870:
say
return
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 866 ⟶ 904:
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">> (env "SHELL")
"/bin/zsh"
> (env "TERM")
"xterm"</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import os
echo getEnv("HOME")</langsyntaxhighlight>
 
=={{header|NSIS}}==
While common environment variables exist as constants within the NSIS script compilation environment [http://nsis.sourceforge.net/Docs/Chapter4.html#4.2.3 (see NSIS documentation)], arbitrarily-named environment variables' values may be retrieved using [http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.2.7 '''ExpandEnvStrings'''].
<langsyntaxhighlight lang="nsis">ExpandEnvStrings $0 "%PATH%" ; Retrieve PATH and place it in builtin register 0.
ExpandEnvStrings $1 "%USERPROFILE%" ; Retrieve the user's profile location and place it in builtin register 1.
ExpandEnvStrings $2 "%USERNAME%" ; Retrieve the user's account name and place it in builtin register 2.</langsyntaxhighlight>
 
=={{header|Nu}}==
<syntaxhighlight lang="nu">
$env.HOME
</syntaxhighlight>
 
=={{header|Objective-C}}==
<code>[[NSProcessInfo processInfo] environment]</code> returns an <tt>NSDictionary</tt> of the current environment.
<langsyntaxhighlight lang="objc">[[[NSProcessInfo processInfo] environment] objectForKey:@"HOME"]</langsyntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">Sys.getenv "HOME"</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">System getEnv("PATH") println</langsyntaxhighlight>
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">{System.showInfo "This is where Mozart is installed: "#{OS.getEnv 'OZHOME'}}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
{{works with|PARI/GP|2.6.0 and above}}
<langsyntaxhighlight lang="parigp">getenv("HOME")</langsyntaxhighlight>
{{works with|PARI/GP|2.4.3 and above}}
<langsyntaxhighlight lang="parigp">externstr("echo $HOME")</langsyntaxhighlight>
{{works with|PARI/GP|2.0.3 and above}}
In older versions, the command must effectively be triple-quoted:
<langsyntaxhighlight lang="parigp">extern("echo \"\\\"$HOME\\\"\"")</langsyntaxhighlight>
The shell sees
<langsyntaxhighlight lang="bash">echo "\"$HOME\""</langsyntaxhighlight>
which causes it to return
<pre>"/home/username"</pre>
Line 910 ⟶ 953:
 
Leaving out the quotation marks allows external commands to return expressions that are then evaluated by GP. For example,
<langsyntaxhighlight lang="parigp">extern("echo Pi")</langsyntaxhighlight>
causes the shell to send Pi back to GP, which interprets the result and returns
<pre>%1 = 3.141592653589793238462643383</pre>
Line 916 ⟶ 959:
=={{header|Perl}}==
The <code>%ENV</code> hash maps environment variables to their values:
<langsyntaxhighlight lang="perl">print $ENV{HOME}, "\n";</langsyntaxhighlight>
 
The <code>POSIX</code>module also has <code>getenv()</code> which is the same thing as a function.
<langsyntaxhighlight lang="perl">use POSIX 'getenv';
print getenv("HOME"),"\n";</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(notonline)-->
<lang Phix>?getenv("PATH")</lang>
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- none such in a browser, that I know of</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">getenv</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"PATH"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: http://rosettacode.org/wiki/Environment_variables
by Galileo, 10/2022 #/
 
def getenv
" > output.txt" chain cmd if "Error!" else "output.txt" "r" fopen dup fgets swap fclose endif
enddef
 
"path" getenv print</syntaxhighlight>
 
=={{header|PHP}}==
The <tt>$_ENV</tt> associative array maps environmental variable names to their values:
<syntaxhighlight lang ="php">$_ENV['HOME']</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">: (sys "TERM")
-> "xterm"
 
: (sys "SHELL")
-> "/bin/bash"</langsyntaxhighlight>
 
=={{header|Pike}}==
<langsyntaxhighlight Pikelang="pike">write("%s\n", getenv("SHELL"));</langsyntaxhighlight>
{{Out}}
<pre>
Line 945 ⟶ 1,001:
=={{header|PowerShell}}==
Environment variables can be found in the Env: drive and are accessed using a special variable syntax:
<syntaxhighlight lang ="powershell">$Env:Path</langsyntaxhighlight>
To get a complete listing of all environment variables one can simply query the appropriate drive for its contents:
<syntaxhighlight lang ="powershell">Get-ChildItem Env:</langsyntaxhighlight>
 
=={{header|Prolog}}==
Line 956 ⟶ 1,012:
=={{header|PureBasic}}==
PureBasic has the built in funtion
<langsyntaxhighlight PureBasiclang="purebasic">GetEnvironmentVariable("Name")</langsyntaxhighlight>
'''Example'''
<langsyntaxhighlight PureBasiclang="purebasic">If OpenConsole()
PrintN("Path:"+#CRLF$ + GetEnvironmentVariable("PATH"))
PrintN(#CRLF$+#CRLF$+"NUMBER_OF_PROCESSORS= "+ GetEnvironmentVariable("NUMBER_OF_PROCESSORS"))
Line 965 ⟶ 1,021:
Input()
CloseConsole()
EndIf</langsyntaxhighlight>
 
=={{header|Python}}==
The <tt>os.environ</tt> dictionary maps environmental variable names to their values:
<langsyntaxhighlight lang="python">import os
os.environ['HOME']</langsyntaxhighlight>
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">Sys.getenv("PATH")</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(getenv "HOME")
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 985 ⟶ 1,041:
{{works with|Rakudo|#24 "Seoul"}}
The <code>%*ENV</code> hash maps environment variables to their values:
<syntaxhighlight lang="raku" perl6line>say %*ENV<HOME>;</langsyntaxhighlight>
 
=={{header|REBOL}}==
<langsyntaxhighlight REBOLlang="rebol">print get-env "HOME"</langsyntaxhighlight>
 
=={{header|Retro}}==
<langsyntaxhighlight Retrolang="retro">here "HOME" getEnv
here puts</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 1,006 ⟶ 1,062:
{{works with|R4}}
{{works with|ROO}}
<langsyntaxhighlight lang="rexx">/*REXX program shows how to get an environmental variable under Windows*/
 
x=value('TEMP',,'SYSTEM')</langsyntaxhighlight>
The following will work for
::* '''PC/REXX'''
Line 1,020 ⟶ 1,076:
{{works with|Regina}}
{{works with|ooRexx}}
<langsyntaxhighlight lang="rexx">/*REXX program shows how to get an environmental variable under Windows*/
 
x=value('TEMP',,'ENVIRONMENT')</langsyntaxhighlight>
 
The brexx interpreter provides a getenv function for accessing environment variables:
{{works with|Brexx}}
<langsyntaxhighlight lang="rexx">x=getenv("PATH") /* Get the contents of the path environment variable */</langsyntaxhighlight>
 
Other REXX interpreters have their own requirements to identify the SYSTEM environment.
Line 1,036 ⟶ 1,092:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see get("path")
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
The <tt>ENV</tt> hash maps environment variable names to their values:
<syntaxhighlight lang ="ruby">ENV['HOME']</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">' ------- Major environment variables -------------------------------------------
'DefaultDir$ - The folder path where program files are read/written by default
'Platform$ - The operating system on which Run BASIC is being hosted
Line 1,064 ⟶ 1,120:
print "User Address is: ";UserAddress$
print "Event Key is : ";EventKey$
print "Default Dir is : ";DefaultDir$</langsyntaxhighlight>
<pre>
{{out}}
Line 1,076 ⟶ 1,132:
 
=={{header|Rust}}==
<langsyntaxhighlight Rustlang="rust">use std::env;
 
fn main() {
Line 1,085 ⟶ 1,141:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>Ok("/root")
Line 1,096 ⟶ 1,152:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">sys.env.get("HOME")</langsyntaxhighlight>
 
=={{header|Seed7}}==
Line 1,106 ⟶ 1,162:
to get the home directory and the search path in an operating system independent manner.
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
const proc: main is func
begin
writeln(getenv("HOME"));
end func;</langsyntaxhighlight>
 
=={{header|Sidef}}==
The ''ENV'' hash maps environment variables to their values:
<syntaxhighlight lang ="ruby">say ENV{'HOME'};</langsyntaxhighlight>
 
=={{header|Slate}}==
<langsyntaxhighlight lang="slate">Environment variables at: 'PATH'.
"==> '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games'"</langsyntaxhighlight>
 
=={{header|Slope}}==
<syntaxhighlight lang="slope">(env "HOME")</syntaxhighlight>
 
=={{header|Smalltalk}}==
Use the [http://pharo.gemtalksystems.com/book/PharoTools/OSProcess OSProcess] library to gain access to environment variables:
 
<langsyntaxhighlight lang="smalltalk">
OSProcess thisOSProcess environment at: #HOME.
OSProcess thisOSProcess environment at: #PATH.
OSProcess thisOSProcess environment at: #USER.
</syntaxhighlight>
</lang>
 
=={{header|SNOBOL4}}==
Line 1,134 ⟶ 1,193:
{{works with|CSnobol}}
The host(4) function returns a known environment variable.
<langsyntaxhighlight SNOBOL4lang="snobol4"> output = host(4,'PATH')
end</langsyntaxhighlight>
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">OS.Process.getEnv "HOME"</langsyntaxhighlight>
returns an option type which is either SOME value or NONE if variable doesn't exist
 
Line 1,144 ⟶ 1,203:
Use the '''env''' [http://www.stata.com/help.cgi?extended_fcn extended macro function].
 
<langsyntaxhighlight lang="stata">display "`:env PATH'"
display "`:env USERNAME'"
display "`:env USERPROFILE'"</langsyntaxhighlight>
 
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">print("USER: \(ProcessInfo.processInfo.environment["USER"] ?? "Not set")")
print("PATH: \(ProcessInfo.processInfo.environment["PATH"] ?? "Not set")")</langsyntaxhighlight>
 
=={{header|True BASIC}}==
<syntaxhighlight lang="qbasic">ASK #1: ACCESS type$
ASK BACK red
ASK COLOR red
ASK COLOR MIX (2) red, green, blue
ASK CURSOR vble1, vble2
ASK #2: DATUM type$
ASK DIRECTORY dir$
ASK #3: ERASABLE type$
ASK #4: FILESIZE vble09
ASK #5: FILETYPE vble10
ASK FREE MEMORY vble11
ASK #61: MARGIN vble12
ASK MAX COLOR vble13
ASK MAX CURSOR vble1, vble2
ASK MODE vble15$
ASK NAME vble16$
ASK #7: ORGANIZATION vble17$
ASK PIXELS vble1, vble2
ASK #8: POINTER vble19$
ASK #9: RECORD vble20
ASK #1: RECSIZE vble21
ASK #2: RECTYPE vble22$
ASK SCREEN vble1, vble2, vble3, vble4
ASK #3: SETTER vble24$
ASK TEXT JUSTIFY vble1$, vble2$
ASK WINDOW vble1, vble2, vble3, vble4
ASK #4: ZONEWIDTH vble27</syntaxhighlight>
 
=={{header|Tcl}}==
The <code>env</code> global array maps environmental variable names to their values:
<syntaxhighlight lang ="tcl">$env(HOME)</langsyntaxhighlight>
 
=={{header|TXR}}==
TXR can treat the environment vector as text stream:
<langsyntaxhighlight lang="txr">@(next :env)
@(collect)
@VAR=@VAL
@(end)</langsyntaxhighlight>
A recently added <code>gather</code> directive is useful for extracting multiple items of data from an unordered stream of this kind (not only the environment vector):
<langsyntaxhighlight lang="txr">@(next :env)
@(gather)
HOME=@home
USER=@user
PATH=@path
@(end)</langsyntaxhighlight>
What if some of the variables might not exist? Gather has some discipline for that. The following means that three variables are required (the gather construct fails if they are not found), but <code>shell</code> is optional with a default value of <code>/bin/sh</code> if it is not extracted from the data:
<langsyntaxhighlight lang="txr">@(next :env)
@(gather :vars (home user path (shell "/bin/sh")))
HOME=@home
Line 1,177 ⟶ 1,265:
PATH=@path
SHELL=@shell
@(end)</langsyntaxhighlight>
From TXR Lisp, the environment is available via the <code>(env)</code> function, which returns a raw list of <code>"name=value</code> strings. The <code>(env-hash)</code> function returns a hash from environment keys to their values.
 
<langsyntaxhighlight lang="bash">$ ./txr -p "(mapcar (env-hash) '(\"HOME\" \"USER\" \"PATH\"))"
("/home/kaz" "kaz" "/home/kaz/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/kaz/bin"</langsyntaxhighlight>
 
Here, the hash is being used as a function to filter several environment keys to their values via <code>mapcar</code>.
Line 1,190 ⟶ 1,278:
=={{header|UNIX Shell}}==
In the Unix Shell Language, environment variables are available as ordinary variables:
<langsyntaxhighlight lang="bash">echo "$HOME"</langsyntaxhighlight>
An ordinary variable can be marked as an environment variable with the <code>export</code> command:
<syntaxhighlight lang ="bash">export VAR</langsyntaxhighlight>
Now child processes launched by the shell will have an environment variable called <code>VAR</code>.
 
Line 1,199 ⟶ 1,287:
 
=={{header|Ursa}}==
<langsyntaxhighlight lang="ursa">import "system"
out (system.getenv "HOME") endl console</langsyntaxhighlight>
 
=={{header|Ursala}}==
The argument to the main program is a record initialized by the run-time system in which one of the fields (environs) contains the environment as a list of key:value pairs.
<langsyntaxhighlight Ursalalang="ursala">#import std
 
#executable ('parameterized','')
 
showenv = <.file$[contents: --<''>]>+ %smP+ ~&n-={'TERM','SHELL','X11BROWSER'}*~+ ~environs</langsyntaxhighlight>
The rest of this application searches for the three variables named
and displays them on standard output.
Line 1,219 ⟶ 1,307:
'SHELL': '/bin/bash',
'X11BROWSER': '/usr/bin/firefox'></pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">// Environment variables in V
// v run environment_variables.v
module main
 
import os
 
pub fn main() {
print('In the $os.environ().len environment variables, ')
println('\$HOME is set to ${os.getenv('HOME')}')
}</syntaxhighlight>
 
{{out}}<pre>prompt$ v run environment-variables.v
In the 64 environment variables, $HOME is set to /home/btiffin</pre>
 
=={{header|Vedit macro language}}==
<langsyntaxhighlight lang="vedit">Get_Environment(10,"PATH")
Message(@10)</langsyntaxhighlight>
Or with short keywords:
<langsyntaxhighlight lang="vedit">GE(10,"PATH") M(@10)</langsyntaxhighlight>
 
=={{header|Visual Basic}}==
{{works with|Visual Basic|VB6 Standard}}
<langsyntaxhighlight lang="vb">Debug.Print Environ$("PATH")</langsyntaxhighlight>
 
=={{header|Wren}}==
Line 1,234 ⟶ 1,337:
 
However, if Wren is embedded in (say) a suitable Go program, then we can ask the latter to obtain it for us.
<langsyntaxhighlight ecmascriptlang="wren">/* environment_variablesEnvironment_variables.wren */
class Environ {
foreign static variable(name)
}
 
System.print(Environ.variable("SHELL"))</langsyntaxhighlight>
 
which we embed in the following Go program and run it.
{{libheader|WrenGo}}
<langsyntaxhighlight lang="go">/* environment_variablesEnvironment_variables.go */
package main
 
Line 1,260 ⟶ 1,363:
func main() {
vm := wren.NewVM()
fileName := "environment_variablesEnvironment_variables.wren"
methodMap := wren.MethodMap{"static variable(_)": getEnvironVariable}
classMap := wren.ClassMap{"Environ": wren.NewClass(nil, nil, methodMap)}
Line 1,267 ⟶ 1,370:
vm.InterpretFile(fileName)
vm.Free()
}</langsyntaxhighlight>
 
{{out}}
Line 1,280 ⟶ 1,383:
version work the same as the 16-bit real-mode versions.
 
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
string 0; \use zero-terminated strings
int CpuReg, PspSeg, EnvSeg, I, J, C;
Line 1,307 ⟶ 1,410:
if Peek(EnvSeg,I) = 0 then quit; \double 0 = env. var. not found
];
]</langsyntaxhighlight>
 
{{out}}
Line 1,313 ⟶ 1,416:
PATH=\masm6;C:\;C:\CXPL;C:\UTIL;C:\DOS;C:\BORLANDC\BIN
</pre>
 
=={{header|Yabasic}}==
<code>peek$("env","NAME")</code> Return the environment variable specified by NAME (which may be any string expression).
 
Which kind of environment variables are available on your system depends, as well as their meaning, on your system; however typing env on the command line will produce a list (for Windows and Unix alike).
 
Note, that peek$("env",...) can be written as peek$("environment",...) too.
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">System.getenv("HOME")
/home/craigd
System.getenv() //--> Dictionary of all env vars</langsyntaxhighlight>
885

edits