Environment variables: Difference between revisions
Content added Content deleted
(Environment variables en Yabasic) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 17: | Line 17: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
<lang |
<syntaxhighlight lang="11l">print(os:getenv(‘HOME’))</syntaxhighlight> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Print a single environment variable. |
Print a single environment variable. |
||
< |
<syntaxhighlight lang="ada">with Ada.Environment_Variables; use Ada.Environment_Variables; |
||
with Ada.Text_Io; use Ada.Text_Io; |
with Ada.Text_Io; use Ada.Text_Io; |
||
Line 27: | Line 27: | ||
begin |
begin |
||
Put_Line("Path : " & Value("PATH")); |
Put_Line("Path : " & Value("PATH")); |
||
end Print_Path;</ |
end Print_Path;</syntaxhighlight> |
||
Print all environment variable names and values. |
Print all environment variable names and values. |
||
< |
<syntaxhighlight lang="ada">with Ada.Environment_Variables; use Ada.Environment_Variables; |
||
with Ada.Text_Io; use Ada.Text_Io; |
with Ada.Text_Io; use Ada.Text_Io; |
||
Line 39: | Line 39: | ||
begin |
begin |
||
Iterate(Print_Vars'access); |
Iterate(Print_Vars'access); |
||
end Env_Vars;</ |
end Env_Vars;</syntaxhighlight> |
||
Line 46: | Line 46: | ||
Uses [http://forge.ada-ru.org/matreshka Matreshka]. |
Uses [http://forge.ada-ru.org/matreshka Matreshka]. |
||
< |
<syntaxhighlight lang="ada">with Ada.Wide_Wide_Text_IO; |
||
with League.Application; |
with League.Application; |
||
Line 60: | Line 60: | ||
Ada.Wide_Wide_Text_IO.Put_Line |
Ada.Wide_Wide_Text_IO.Put_Line |
||
(League.Application.Environment.Value (+"HOME").To_Wide_Wide_String); |
(League.Application.Environment.Value (+"HOME").To_Wide_Wide_String); |
||
end Main;</ |
end Main;</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{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}} |
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386 - ''getenv'' is not part of the standard's prelude}} |
||
< |
<syntaxhighlight lang="algol68">print((getenv("HOME"), new line))</syntaxhighlight> |
||
=={{header|APL}}== |
=={{header|APL}}== |
||
<syntaxhighlight lang="apl"> |
|||
<lang APL> |
|||
⎕ENV 'HOME' |
⎕ENV 'HOME' |
||
HOME /home/russtopia |
HOME /home/russtopia |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
===Invoking Finder=== |
===Invoking Finder=== |
||
<syntaxhighlight lang="applescript"> |
|||
<lang AppleScript> |
|||
tell application "Finder" to get name of home |
tell application "Finder" to get name of home |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Invoking Terminal=== |
===Invoking Terminal=== |
||
<syntaxhighlight lang="applescript"> |
|||
<lang AppleScript> |
|||
"HOME : " & (do shell script "echo $HOME" & ", PATH : " & (do shell script "echo $PATH" & ", USER : " & (do shell script "echo $USER"))) |
"HOME : " & (do shell script "echo $HOME" & ", PATH : " & (do shell script "echo $PATH" & ", USER : " & (do shell script "echo $USER"))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">print ["path:" env\PATH] |
||
print ["user:" env\USER] |
print ["user:" env\USER] |
||
print ["home:" env\HOME]</ |
print ["home:" env\HOME]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 95: | Line 95: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">EnvGet, OutputVar, Path |
||
MsgBox, %OutputVar%</ |
MsgBox, %OutputVar%</syntaxhighlight> |
||
=={{header|AutoIt}}== |
=={{header|AutoIt}}== |
||
< |
<syntaxhighlight lang="autoit">ConsoleWrite("# Environment:" & @CRLF) |
||
Local $sEnvVar = EnvGet("LANG") |
Local $sEnvVar = EnvGet("LANG") |
||
Line 109: | Line 109: | ||
Func ShowEnv($N) |
Func ShowEnv($N) |
||
ConsoleWrite( StringFormat("%-12s : %s\n", $N, EnvGet($N)) ) |
ConsoleWrite( StringFormat("%-12s : %s\n", $N, EnvGet($N)) ) |
||
EndFunc ;==>ShowEnv</ |
EndFunc ;==>ShowEnv</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 119: | Line 119: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
The ENVIRON array contains the values of the current environment: |
The ENVIRON array contains the values of the current environment: |
||
< |
<syntaxhighlight lang="awk">$ awk 'BEGIN{print "HOME:"ENVIRON["HOME"],"USER:"ENVIRON["USER"]}' </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 125: | Line 125: | ||
Environment variables can also be assigned to awk variables before execution, with (-v) options: |
Environment variables can also be assigned to awk variables before execution, with (-v) options: |
||
< |
<syntaxhighlight lang="awk">$ awk -v h=$HOME -v u=$USER 'BEGIN{print "HOME:"h,"USER:"u}' </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>HOME:/home/suchrich USER:SuchRich</pre> |
<pre>HOME:/home/suchrich USER:SuchRich</pre> |
||
Listing all the environment variables: |
Listing all the environment variables: |
||
< |
<syntaxhighlight lang="awk"># http://ideone.com/St5SHF |
||
BEGIN { print "# Environment:" |
BEGIN { print "# Environment:" |
||
for (e in ENVIRON) { printf( "%10s = %s\n", e, ENVIRON[e] ) } |
for (e in ENVIRON) { printf( "%10s = %s\n", e, ENVIRON[e] ) } |
||
} |
} |
||
END { print "# Done." } </ |
END { print "# Done." } </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 151: | Line 151: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
< |
<syntaxhighlight lang="qbasic">x$ = ENVIRON$("path") |
||
PRINT x$</ |
PRINT x$</syntaxhighlight> |
||
==={{header|BaCon}}=== |
==={{header|BaCon}}=== |
||
''Case matters and needs to match'' |
''Case matters and needs to match'' |
||
< |
<syntaxhighlight lang="freebasic">PRINT GETENVIRON$("PATH")</syntaxhighlight> |
||
==={{header|IS-BASIC}}=== |
==={{header|IS-BASIC}}=== |
||
< |
<syntaxhighlight lang="is-basic">100 ASK BORDER VAR01 |
||
110 ASK DEFAULT CHANNEL VAR02 |
110 ASK DEFAULT CHANNEL VAR02 |
||
120 ASK EDITOR BUFFER VAR03 |
120 ASK EDITOR BUFFER VAR03 |
||
Line 186: | Line 186: | ||
350 ASK VIDEO MODE VAR26 |
350 ASK VIDEO MODE VAR26 |
||
360 ASK VIDEO X VAR27 |
360 ASK VIDEO X VAR27 |
||
370 ASK VIDEO Y VAR28</ |
370 ASK VIDEO Y VAR28</syntaxhighlight> |
||
or |
or |
||
<lang |
<syntaxhighlight lang="is-basic">ASK machine-option-code var</syntaxhighlight> |
||
==={{header|ZX Spectrum Basic}}=== |
==={{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: |
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: |
||
< |
<syntaxhighlight 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 |
20 PRINT "The ramtop address is "; PEEK (23730) + 256 * PEEK (23731): REM ramtop |
||
30 POKE 23609,50: REM set keyboard pip to 50</ |
30 POKE 23609,50: REM set keyboard pip to 50</syntaxhighlight> |
||
=={{header|Batch File}}== |
=={{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: |
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: |
||
<lang |
<syntaxhighlight lang="dos">echo %Foo%</syntaxhighlight> |
||
For interactive use one can use <code>set</code> to view all environment variables or all variables starting with a certain string: |
For interactive use one can use <code>set</code> to view all environment variables or all variables starting with a certain string: |
||
< |
<syntaxhighlight lang="dos">set |
||
set Foo</ |
set Foo</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> PRINT FNenvironment("PATH") |
||
PRINT FNenvironment("USERNAME") |
PRINT FNenvironment("USERNAME") |
||
END |
END |
||
Line 216: | Line 216: | ||
DIM buffer% LOCAL size% |
DIM buffer% LOCAL size% |
||
SYS "GetEnvironmentVariable", envar$, buffer%, size%+1 |
SYS "GetEnvironmentVariable", envar$, buffer%, size%+1 |
||
= $$buffer%</ |
= $$buffer%</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdlib.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 228: | Line 228: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
namespace RosettaCode { |
namespace RosettaCode { |
||
Line 240: | Line 240: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <cstdlib> |
||
#include <cstdio> |
#include <cstdio> |
||
Line 250: | Line 250: | ||
puts(getenv("HOME")); |
puts(getenv("HOME")); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="lisp">(System/getenv "HOME")</syntaxhighlight> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
< |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
PROGRAM-ID. Environment-Vars. |
PROGRAM-ID. Environment-Vars. |
||
Line 273: | Line 273: | ||
GOBACK |
GOBACK |
||
.</ |
.</syntaxhighlight> |
||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
{{works with|node.js}} |
{{works with|node.js}} |
||
< |
<syntaxhighlight lang="coffeescript">for var_name in ['PATH', 'HOME', 'LANG', 'USER'] |
||
console.log var_name, process.env[var_name]</ |
console.log var_name, process.env[var_name]</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{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. |
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}} |
{{works with|LispWorks}} |
||
< |
<syntaxhighlight lang="lisp">(lispworks:environment-variable "USER")</syntaxhighlight> |
||
{{works with|SBCL}} |
{{works with|SBCL}} |
||
< |
<syntaxhighlight lang="lisp">(sb-ext:posix-getenv "USER")</syntaxhighlight> |
||
{{works with|Clozure CL}} |
{{works with|Clozure CL}} |
||
< |
<syntaxhighlight lang="lisp">(ccl:getenv "USER")</syntaxhighlight> |
||
{{works with|CLISP}} |
{{works with|CLISP}} |
||
< |
<syntaxhighlight lang="lisp">(getenv "HOME")</syntaxhighlight> |
||
Ways to do this in some other implementations are listed in the [http://cl-cookbook.sourceforge.net/os.html#env Common Lisp Cookbook]. |
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}}== |
=={{header|D}}== |
||
{{libheader|phobos}} |
{{libheader|phobos}} |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.process; |
||
void main() { |
void main() { |
||
auto home = getenv("HOME"); |
auto home = getenv("HOME"); |
||
}</ |
}</syntaxhighlight> |
||
{{libheader|tango}} |
{{libheader|tango}} |
||
< |
<syntaxhighlight lang="d">import tango.sys.Environment; |
||
void main() { |
void main() { |
||
auto home = Environment("HOME"); |
auto home = Environment("HOME"); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Delphi}}/{{header|Pascal}}== |
=={{header|Delphi}}/{{header|Pascal}}== |
||
< |
<syntaxhighlight lang="delphi">program EnvironmentVariable; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 315: | Line 315: | ||
begin |
begin |
||
WriteLn('Temp = ' + GetEnvironmentVariable('TEMP')); |
WriteLn('Temp = ' + GetEnvironmentVariable('TEMP')); |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|E}}== |
=={{header|E}}== |
||
{{works with|E-on-Java}} |
{{works with|E-on-Java}} |
||
< |
<syntaxhighlight lang="e"><unsafe:java.lang.System>.getenv("HOME")</syntaxhighlight> |
||
=={{header|Eiffel}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="eiffel ">class |
||
APPLICATION |
APPLICATION |
||
inherit |
inherit |
||
Line 335: | Line 335: | ||
print (get ("USERNAME")) |
print (get ("USERNAME")) |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">System.get_env("PATH")</syntaxhighlight> |
||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(getenv "HOME")</syntaxhighlight> |
||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
os:getenv( "HOME" ). |
os:getenv( "HOME" ). |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang="euphoria">puts(1,getenv("PATH"))</syntaxhighlight> |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">open System |
||
[<EntryPoint>] |
[<EntryPoint>] |
||
let main args = |
let main args = |
||
printfn "%A" (Environment.GetEnvironmentVariable("PATH")) |
printfn "%A" (Environment.GetEnvironmentVariable("PATH")) |
||
0</ |
0</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">"HOME" os-env print</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
{{works with|GNU Forth}} |
{{works with|GNU Forth}} |
||
< |
<syntaxhighlight lang="forth">s" HOME" getenv type</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|any Fortran compiler}} |
{{works with|any Fortran compiler}} |
||
< |
<syntaxhighlight lang="fortran">program show_home |
||
implicit none |
implicit none |
||
character(len=32) :: home_val ! The string value of the variable HOME |
character(len=32) :: home_val ! The string value of the variable HOME |
||
Line 382: | Line 382: | ||
write(*,'(a)') 'No HOME to go to!' |
write(*,'(a)') 'No HOME to go to!' |
||
end if |
end if |
||
end program show_home</ |
end program show_home</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Var v = Environ("SystemRoot") |
Var v = Environ("SystemRoot") |
||
Print v |
Print v |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 398: | Line 398: | ||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
< |
<syntaxhighlight lang="funl">callJava["java.lang.System", "getenv", ["HOME"]]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 406: | Line 406: | ||
=={{header|FunL}}== |
=={{header|FunL}}== |
||
< |
<syntaxhighlight lang="funl">println( System.getenv('PATH') ) |
||
println( $home ) |
println( $home ) |
||
println( $user )</ |
println( $user )</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
;Simply: |
;Simply: |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 421: | Line 421: | ||
func main() { |
func main() { |
||
fmt.Println(os.Getenv("SHELL")) |
fmt.Println(os.Getenv("SHELL")) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 430: | Line 430: | ||
You're on your own then to parse out the one you want. |
You're on your own then to parse out the one you want. |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 448: | Line 448: | ||
} |
} |
||
fmt.Println(s, "not found") |
fmt.Println(s, "not found") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 456: | Line 456: | ||
=={{header|Gri}}== |
=={{header|Gri}}== |
||
Command <code>get env</code> fetches an environment variable into a synonym (a string) |
Command <code>get env</code> fetches an environment variable into a synonym (a string) |
||
< |
<syntaxhighlight lang="gri">get env \foo HOME |
||
show "\foo"</ |
show "\foo"</syntaxhighlight> |
||
Quotes can be used in the usual way if the environment variable name contains spaces (which is unusual, but possible). |
Quotes can be used in the usual way if the environment variable name contains spaces (which is unusual, but possible). |
||
< |
<syntaxhighlight lang="gri">get env \foo "X Y Z"</syntaxhighlight> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy">System.getenv().each { property, value -> println "$property = $value"}</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import System.Environment |
||
main = do getEnv "HOME" >>= print -- get env var |
main = do getEnv "HOME" >>= print -- get env var |
||
getEnvironment >>= print -- get the entire environment as a list of (key, value) pairs</ |
getEnvironment >>= print -- get the entire environment as a list of (key, value) pairs</syntaxhighlight> |
||
=={{header|hexiscript}}== |
=={{header|hexiscript}}== |
||
< |
<syntaxhighlight lang="hexiscript">println env "HOME" |
||
println env "PATH" |
println env "PATH" |
||
println env "USER"</ |
println env "USER"</syntaxhighlight> |
||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
< |
<syntaxhighlight lang="hicest">CHARACTER string*255 |
||
string = "PATH=" |
string = "PATH=" |
||
SYSTEM(GEteNV = string)</ |
SYSTEM(GEteNV = string)</syntaxhighlight> |
||
=={{header|i}}== |
=={{header|i}}== |
||
< |
<syntaxhighlight lang="i">software { |
||
print(load("$HOME")) |
print(load("$HOME")) |
||
print(load("$USER")) |
print(load("$USER")) |
||
print(load("$PATH")) |
print(load("$PATH")) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
{{works with|Unicon}} |
{{works with|Unicon}} |
||
< |
<syntaxhighlight lang="icon">procedure main(arglist) |
||
if *envars = 0 then envars := ["HOME", "TRACE", "BLKSIZE","STRSIZE","COEXPSIZE","MSTKSIZE", "IPATH","LPATH","NOERRBUF"] |
if *envars = 0 then envars := ["HOME", "TRACE", "BLKSIZE","STRSIZE","COEXPSIZE","MSTKSIZE", "IPATH","LPATH","NOERRBUF"] |
||
Line 496: | Line 496: | ||
every v := !sort(envars) do |
every v := !sort(envars) do |
||
write(v," = ",image(getenv(v))|"* not set *") |
write(v," = ",image(getenv(v))|"* not set *") |
||
end</ |
end</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
<lang |
<syntaxhighlight lang="j">2!:5'HOME'</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">System.getenv("HOME") // get env var |
||
System.getenv() // get the entire environment as a Map of keys to values</ |
System.getenv() // get the entire environment as a Map of keys to values</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
The JavaScript language has no facilities to access the computer: it relies on the host environment to provide it. |
The JavaScript language has no facilities to access the computer: it relies on the host environment to provide it. |
||
{{works with|JScript}} |
{{works with|JScript}} |
||
< |
<syntaxhighlight lang="javascript">var shell = new ActiveXObject("WScript.Shell"); |
||
var env = shell.Environment("PROCESS"); |
var env = shell.Environment("PROCESS"); |
||
WScript.echo('SYSTEMROOT=' + env.item('SYSTEMROOT'));</ |
WScript.echo('SYSTEMROOT=' + env.item('SYSTEMROOT'));</syntaxhighlight> |
||
=={{header|Joy}}== |
=={{header|Joy}}== |
||
< |
<syntaxhighlight lang="joy">"HOME" getenv.</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
<lang |
<syntaxhighlight lang="jq">env.HOME</syntaxhighlight>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}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="javascript">/* Environment variables, in Jsi */ |
||
puts(Util.getenv("HOME")); |
puts(Util.getenv("HOME")); |
||
var environment = Util.getenv(); |
var environment = Util.getenv(); |
||
puts(environment.PATH);</ |
puts(environment.PATH);</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{works with|Julia|0.6}} |
{{works with|Julia|0.6}} |
||
< |
<syntaxhighlight lang="julia">@show ENV["PATH"] |
||
@show ENV["HOME"] |
@show ENV["HOME"] |
||
@show ENV["USER"]</ |
@show ENV["USER"]</syntaxhighlight> |
||
=={{header|K}}== |
=={{header|K}}== |
||
< |
<syntaxhighlight lang="k">_getenv "HOME"</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
// tested on Windows 10 |
// tested on Windows 10 |
||
Line 542: | Line 542: | ||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
println(System.getenv("SystemRoot")) |
println(System.getenv("SystemRoot")) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 550: | Line 550: | ||
=={{header|langur}}== |
=={{header|langur}}== |
||
< |
<syntaxhighlight lang="langur">writeln "HOME: ", _env["HOME"] |
||
writeln "PATH: ", _env["PATH"] |
writeln "PATH: ", _env["PATH"] |
||
writeln "USER: ", _env["USER"]</ |
writeln "USER: ", _env["USER"]</syntaxhighlight> |
||
{{works with|langur|0.9}} |
{{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. |
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. |
||
< |
<syntaxhighlight lang="langur">writeln "HOME: ", _env'HOME |
||
writeln "PATH: ", _env'PATH |
writeln "PATH: ", _env'PATH |
||
writeln "USER: ", _env'USER</ |
writeln "USER: ", _env'USER</syntaxhighlight> |
||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">#!/usr/bin/lasso9 |
||
define getenv(sysvar::string) => { |
define getenv(sysvar::string) => { |
||
Line 575: | Line 575: | ||
stdoutnl(getenv('PATH')) |
stdoutnl(getenv('PATH')) |
||
stdoutnl(getenv('USER')) |
stdoutnl(getenv('USER')) |
||
stdoutnl(getenv('WHAT'))</ |
stdoutnl(getenv('WHAT'))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>/Users/rosetta |
<pre>/Users/rosetta |
||
Line 584: | Line 584: | ||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
=== Built-in variables === |
=== Built-in variables === |
||
< |
<syntaxhighlight lang="lb">print StartupDir$ |
||
print DefaultDir$</ |
print DefaultDir$</syntaxhighlight> |
||
=== Other variables === |
=== Other variables === |
||
< |
<syntaxhighlight lang="lb">print GetEnvironmentVariable$("USERNAME") |
||
print GetEnvironmentVariable$("USERPROFILE") ' equivalent to UNIX HOME variable |
print GetEnvironmentVariable$("USERPROFILE") ' equivalent to UNIX HOME variable |
||
print GetEnvironmentVariable$("PATH") |
print GetEnvironmentVariable$("PATH") |
||
Line 615: | Line 615: | ||
GetEnvironmentVariable$ = left$(lpBuffer$, result) |
GetEnvironmentVariable$ = left$(lpBuffer$, result) |
||
end select |
end select |
||
end function</ |
end function</syntaxhighlight> |
||
=={{header|LIL}}== |
=={{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. |
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) |
static LILCALLBACK lil_value_t fnc_env(lil_t lil, size_t argc, lil_value_t* argv) |
||
{ |
{ |
||
if (!argc) return NULL; |
if (!argc) return NULL; |
||
return lil_alloc_string(getenv(lil_to_string(argv[0]))); |
return lil_alloc_string(getenv(lil_to_string(argv[0]))); |
||
}</ |
}</syntaxhighlight> |
||
Then inside the main functions for repl and nonint (Interactive, Noninteractive): |
Then inside the main functions for repl and nonint (Interactive, Noninteractive): |
||
< |
<syntaxhighlight lang="c">lil_register(lil, "env", fnc_env);</syntaxhighlight> |
||
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. |
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 644: | Line 644: | ||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
{{libheader|Shell Xtra}} |
{{libheader|Shell Xtra}} |
||
< |
<syntaxhighlight lang="lingo">sx = xtra("Shell").new() |
||
if the platform contains "win" then |
if the platform contains "win" then |
||
path = sx.shell_cmd("echo %PATH%").line[1] |
path = sx.shell_cmd("echo %PATH%").line[1] |
||
else |
else |
||
path = sx.shell_cmd("echo $PATH").line[1] |
path = sx.shell_cmd("echo $PATH").line[1] |
||
end if</ |
end if</syntaxhighlight> |
||
=={{header|Logtalk}}== |
=={{header|Logtalk}}== |
||
Using the standard library: |
Using the standard library: |
||
< |
<syntaxhighlight lang="logtalk">os::environment_variable('PATH', Path).</syntaxhighlight> |
||
=={{header|LSL}}== |
=={{header|LSL}}== |
||
Rez a box on the ground, and add the following as a New Script. |
Rez a box on the ground, and add the following as a New Script. |
||
< |
<syntaxhighlight lang="lsl">default { |
||
state_entry() { |
state_entry() { |
||
llOwnerSay("llGetTimestamp()="+(string)llGetTimestamp()); |
llOwnerSay("llGetTimestamp()="+(string)llGetTimestamp()); |
||
Line 664: | Line 664: | ||
llOwnerSay("llGetMemoryLimit()="+(string)llGetMemoryLimit()); |
llOwnerSay("llGetMemoryLimit()="+(string)llGetMemoryLimit()); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>llGetTimestamp()=2012-07-18T01:26:12.133137Z |
<pre>llGetTimestamp()=2012-07-18T01:26:12.133137Z |
||
Line 672: | Line 672: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">print( os.getenv( "PATH" ) )</syntaxhighlight> |
||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Module CheckIt { |
Module CheckIt { |
||
\\ using read only variablles |
\\ using read only variablles |
||
Line 702: | Line 702: | ||
} |
} |
||
Checkit |
Checkit |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Make}}== |
=={{header|Make}}== |
||
Make variables are initialized from the environment, so simply |
Make variables are initialized from the environment, so simply |
||
< |
<syntaxhighlight lang="make">TARGET = $(HOME)/some/thing.txt |
||
foo: |
foo: |
||
echo $(TARGET)</ |
echo $(TARGET)</syntaxhighlight> |
||
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. |
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. |
||
< |
<syntaxhighlight lang="make">bar: |
||
echo "$$HOME"</ |
echo "$$HOME"</syntaxhighlight> |
||
If you mistakenly write just <code>$HOME</code> then it means the makefile <code>$H</code> followed by characters <code>OME</code>. |
If you mistakenly write just <code>$HOME</code> then it means the makefile <code>$H</code> followed by characters <code>OME</code>. |
||
< |
<syntaxhighlight lang="make">H = oops ... |
||
bar: |
bar: |
||
echo $HOME |
echo $HOME |
||
# prints oops ... OME</ |
# prints oops ... OME</syntaxhighlight> |
||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">getenv("PATH");</syntaxhighlight> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">Environment["PATH"]</syntaxhighlight> |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab"> getenv('HOME') |
||
getenv('PATH') |
getenv('PATH') |
||
getenv('USER')</ |
getenv('USER')</syntaxhighlight> |
||
=={{header|Mercury}}== |
=={{header|Mercury}}== |
||
< |
<syntaxhighlight lang="mercury">:- module env_var. |
||
:- interface. |
:- interface. |
||
Line 751: | Line 751: | ||
MaybeValue = no, |
MaybeValue = no, |
||
io.write_string("environment variable HOME not set\n", !IO) |
io.write_string("environment variable HOME not set\n", !IO) |
||
).</ |
).</syntaxhighlight> |
||
=={{header|min}}== |
=={{header|min}}== |
||
{{works with|min|0.19.3}} |
{{works with|min|0.19.3}} |
||
<lang |
<syntaxhighlight lang="min">$PATH</syntaxhighlight> |
||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
< |
<syntaxhighlight lang="modula3">MODULE EnvVars EXPORTS Main; |
||
IMPORT IO, Env; |
IMPORT IO, Env; |
||
Line 772: | Line 772: | ||
IO.Put(k & " = " & v & "\n") |
IO.Put(k & " = " & v & "\n") |
||
END |
END |
||
END EnvVars.</ |
END EnvVars.</syntaxhighlight> |
||
=={{header|MUMPS}}== |
=={{header|MUMPS}}== |
||
Line 778: | Line 778: | ||
In Caché on OpenVMS in an FILES-11 filesystem ODS-5 mode these could work: |
In Caché on OpenVMS in an FILES-11 filesystem ODS-5 mode these could work: |
||
< |
<syntaxhighlight lang="mumps"> Set X=$ZF(-1,"show logical") |
||
Set X=$ZF(-1,"show symbol")</ |
Set X=$ZF(-1,"show symbol")</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
When NetRexx runs under a JVM, system ENVIRONMENT variables are complimented by JVM system properties. This sample shows how to get both. |
When NetRexx runs under a JVM, system ENVIRONMENT variables are complimented by JVM system properties. This sample shows how to get both. |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 843: | Line 843: | ||
say |
say |
||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 877: | Line 877: | ||
=={{header|NewLISP}}== |
=={{header|NewLISP}}== |
||
< |
<syntaxhighlight lang="newlisp">> (env "SHELL") |
||
"/bin/zsh" |
"/bin/zsh" |
||
> (env "TERM") |
> (env "TERM") |
||
"xterm"</ |
"xterm"</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import os |
||
echo getEnv("HOME")</ |
echo getEnv("HOME")</syntaxhighlight> |
||
=={{header|NSIS}}== |
=={{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''']. |
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''']. |
||
< |
<syntaxhighlight 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 $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.</ |
ExpandEnvStrings $2 "%USERNAME%" ; Retrieve the user's account name and place it in builtin register 2.</syntaxhighlight> |
||
=={{header|Objective-C}}== |
=={{header|Objective-C}}== |
||
<code>[[NSProcessInfo processInfo] environment]</code> returns an <tt>NSDictionary</tt> of the current environment. |
<code>[[NSProcessInfo processInfo] environment]</code> returns an <tt>NSDictionary</tt> of the current environment. |
||
< |
<syntaxhighlight lang="objc">[[[NSProcessInfo processInfo] environment] objectForKey:@"HOME"]</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">Sys.getenv "HOME"</syntaxhighlight> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">System getEnv("PATH") println</syntaxhighlight> |
||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
< |
<syntaxhighlight lang="oz">{System.showInfo "This is where Mozart is installed: "#{OS.getEnv 'OZHOME'}}</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
{{works with|PARI/GP|2.6.0 and above}} |
{{works with|PARI/GP|2.6.0 and above}} |
||
< |
<syntaxhighlight lang="parigp">getenv("HOME")</syntaxhighlight> |
||
{{works with|PARI/GP|2.4.3 and above}} |
{{works with|PARI/GP|2.4.3 and above}} |
||
< |
<syntaxhighlight lang="parigp">externstr("echo $HOME")</syntaxhighlight> |
||
{{works with|PARI/GP|2.0.3 and above}} |
{{works with|PARI/GP|2.0.3 and above}} |
||
In older versions, the command must effectively be triple-quoted: |
In older versions, the command must effectively be triple-quoted: |
||
< |
<syntaxhighlight lang="parigp">extern("echo \"\\\"$HOME\\\"\"")</syntaxhighlight> |
||
The shell sees |
The shell sees |
||
< |
<syntaxhighlight lang="bash">echo "\"$HOME\""</syntaxhighlight> |
||
which causes it to return |
which causes it to return |
||
<pre>"/home/username"</pre> |
<pre>"/home/username"</pre> |
||
Line 921: | Line 921: | ||
Leaving out the quotation marks allows external commands to return expressions that are then evaluated by GP. For example, |
Leaving out the quotation marks allows external commands to return expressions that are then evaluated by GP. For example, |
||
< |
<syntaxhighlight lang="parigp">extern("echo Pi")</syntaxhighlight> |
||
causes the shell to send Pi back to GP, which interprets the result and returns |
causes the shell to send Pi back to GP, which interprets the result and returns |
||
<pre>%1 = 3.141592653589793238462643383</pre> |
<pre>%1 = 3.141592653589793238462643383</pre> |
||
Line 927: | Line 927: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
The <code>%ENV</code> hash maps environment variables to their values: |
The <code>%ENV</code> hash maps environment variables to their values: |
||
< |
<syntaxhighlight lang="perl">print $ENV{HOME}, "\n";</syntaxhighlight> |
||
The <code>POSIX</code>module also has <code>getenv()</code> which is the same thing as a function. |
The <code>POSIX</code>module also has <code>getenv()</code> which is the same thing as a function. |
||
< |
<syntaxhighlight lang="perl">use POSIX 'getenv'; |
||
print getenv("HOME"),"\n";</ |
print getenv("HOME"),"\n";</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(notonline)--> |
||
<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: #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> |
<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|PHP}}== |
=={{header|PHP}}== |
||
The <tt>$_ENV</tt> associative array maps environmental variable names to their values: |
The <tt>$_ENV</tt> associative array maps environmental variable names to their values: |
||
<lang |
<syntaxhighlight lang="php">$_ENV['HOME']</syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">: (sys "TERM") |
||
-> "xterm" |
-> "xterm" |
||
: (sys "SHELL") |
: (sys "SHELL") |
||
-> "/bin/bash"</ |
-> "/bin/bash"</syntaxhighlight> |
||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
< |
<syntaxhighlight lang="pike">write("%s\n", getenv("SHELL"));</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 959: | Line 959: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
Environment variables can be found in the Env: drive and are accessed using a special variable syntax: |
Environment variables can be found in the Env: drive and are accessed using a special variable syntax: |
||
<lang |
<syntaxhighlight lang="powershell">$Env:Path</syntaxhighlight> |
||
To get a complete listing of all environment variables one can simply query the appropriate drive for its contents: |
To get a complete listing of all environment variables one can simply query the appropriate drive for its contents: |
||
<lang |
<syntaxhighlight lang="powershell">Get-ChildItem Env:</syntaxhighlight> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Line 970: | Line 970: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
PureBasic has the built in funtion |
PureBasic has the built in funtion |
||
< |
<syntaxhighlight lang="purebasic">GetEnvironmentVariable("Name")</syntaxhighlight> |
||
'''Example''' |
'''Example''' |
||
< |
<syntaxhighlight lang="purebasic">If OpenConsole() |
||
PrintN("Path:"+#CRLF$ + GetEnvironmentVariable("PATH")) |
PrintN("Path:"+#CRLF$ + GetEnvironmentVariable("PATH")) |
||
PrintN(#CRLF$+#CRLF$+"NUMBER_OF_PROCESSORS= "+ GetEnvironmentVariable("NUMBER_OF_PROCESSORS")) |
PrintN(#CRLF$+#CRLF$+"NUMBER_OF_PROCESSORS= "+ GetEnvironmentVariable("NUMBER_OF_PROCESSORS")) |
||
Line 979: | Line 979: | ||
Input() |
Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
The <tt>os.environ</tt> dictionary maps environmental variable names to their values: |
The <tt>os.environ</tt> dictionary maps environmental variable names to their values: |
||
< |
<syntaxhighlight lang="python">import os |
||
os.environ['HOME']</ |
os.environ['HOME']</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">Sys.getenv("PATH")</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(getenv "HOME") |
(getenv "HOME") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 999: | Line 999: | ||
{{works with|Rakudo|#24 "Seoul"}} |
{{works with|Rakudo|#24 "Seoul"}} |
||
The <code>%*ENV</code> hash maps environment variables to their values: |
The <code>%*ENV</code> hash maps environment variables to their values: |
||
<lang |
<syntaxhighlight lang="raku" line>say %*ENV<HOME>;</syntaxhighlight> |
||
=={{header|REBOL}}== |
=={{header|REBOL}}== |
||
< |
<syntaxhighlight lang="rebol">print get-env "HOME"</syntaxhighlight> |
||
=={{header|Retro}}== |
=={{header|Retro}}== |
||
< |
<syntaxhighlight lang="retro">here "HOME" getEnv |
||
here puts</ |
here puts</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 1,020: | Line 1,020: | ||
{{works with|R4}} |
{{works with|R4}} |
||
{{works with|ROO}} |
{{works with|ROO}} |
||
< |
<syntaxhighlight lang="rexx">/*REXX program shows how to get an environmental variable under Windows*/ |
||
x=value('TEMP',,'SYSTEM')</ |
x=value('TEMP',,'SYSTEM')</syntaxhighlight> |
||
The following will work for |
The following will work for |
||
::* '''PC/REXX''' |
::* '''PC/REXX''' |
||
Line 1,034: | Line 1,034: | ||
{{works with|Regina}} |
{{works with|Regina}} |
||
{{works with|ooRexx}} |
{{works with|ooRexx}} |
||
< |
<syntaxhighlight lang="rexx">/*REXX program shows how to get an environmental variable under Windows*/ |
||
x=value('TEMP',,'ENVIRONMENT')</ |
x=value('TEMP',,'ENVIRONMENT')</syntaxhighlight> |
||
The brexx interpreter provides a getenv function for accessing environment variables: |
The brexx interpreter provides a getenv function for accessing environment variables: |
||
{{works with|Brexx}} |
{{works with|Brexx}} |
||
< |
<syntaxhighlight lang="rexx">x=getenv("PATH") /* Get the contents of the path environment variable */</syntaxhighlight> |
||
Other REXX interpreters have their own requirements to identify the SYSTEM environment. |
Other REXX interpreters have their own requirements to identify the SYSTEM environment. |
||
Line 1,050: | Line 1,050: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
see get("path") |
see get("path") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
The <tt>ENV</tt> hash maps environment variable names to their values: |
The <tt>ENV</tt> hash maps environment variable names to their values: |
||
<lang |
<syntaxhighlight lang="ruby">ENV['HOME']</syntaxhighlight> |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">' ------- Major environment variables ------------------------------------------- |
||
'DefaultDir$ - The folder path where program files are read/written by default |
'DefaultDir$ - The folder path where program files are read/written by default |
||
'Platform$ - The operating system on which Run BASIC is being hosted |
'Platform$ - The operating system on which Run BASIC is being hosted |
||
Line 1,078: | Line 1,078: | ||
print "User Address is: ";UserAddress$ |
print "User Address is: ";UserAddress$ |
||
print "Event Key is : ";EventKey$ |
print "Event Key is : ";EventKey$ |
||
print "Default Dir is : ";DefaultDir$</ |
print "Default Dir is : ";DefaultDir$</syntaxhighlight> |
||
<pre> |
<pre> |
||
{{out}} |
{{out}} |
||
Line 1,090: | Line 1,090: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">use std::env; |
||
fn main() { |
fn main() { |
||
Line 1,099: | Line 1,099: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Ok("/root") |
<pre>Ok("/root") |
||
Line 1,110: | Line 1,110: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">sys.env.get("HOME")</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
Line 1,120: | Line 1,120: | ||
to get the home directory and the search path in an operating system independent manner. |
to get the home directory and the search path in an operating system independent manner. |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const proc: main is func |
const proc: main is func |
||
begin |
begin |
||
writeln(getenv("HOME")); |
writeln(getenv("HOME")); |
||
end func;</ |
end func;</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
The ''ENV'' hash maps environment variables to their values: |
The ''ENV'' hash maps environment variables to their values: |
||
<lang |
<syntaxhighlight lang="ruby">say ENV{'HOME'};</syntaxhighlight> |
||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
< |
<syntaxhighlight lang="slate">Environment variables at: 'PATH'. |
||
"==> '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games'"</ |
"==> '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games'"</syntaxhighlight> |
||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
Use the [http://pharo.gemtalksystems.com/book/PharoTools/OSProcess OSProcess] library to gain access to environment variables: |
Use the [http://pharo.gemtalksystems.com/book/PharoTools/OSProcess OSProcess] library to gain access to environment variables: |
||
< |
<syntaxhighlight lang="smalltalk"> |
||
OSProcess thisOSProcess environment at: #HOME. |
OSProcess thisOSProcess environment at: #HOME. |
||
OSProcess thisOSProcess environment at: #PATH. |
OSProcess thisOSProcess environment at: #PATH. |
||
OSProcess thisOSProcess environment at: #USER. |
OSProcess thisOSProcess environment at: #USER. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
Line 1,148: | Line 1,148: | ||
{{works with|CSnobol}} |
{{works with|CSnobol}} |
||
The host(4) function returns a known environment variable. |
The host(4) function returns a known environment variable. |
||
< |
<syntaxhighlight lang="snobol4"> output = host(4,'PATH') |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
< |
<syntaxhighlight lang="sml">OS.Process.getEnv "HOME"</syntaxhighlight> |
||
returns an option type which is either SOME value or NONE if variable doesn't exist |
returns an option type which is either SOME value or NONE if variable doesn't exist |
||
Line 1,158: | Line 1,158: | ||
Use the '''env''' [http://www.stata.com/help.cgi?extended_fcn extended macro function]. |
Use the '''env''' [http://www.stata.com/help.cgi?extended_fcn extended macro function]. |
||
< |
<syntaxhighlight lang="stata">display "`:env PATH'" |
||
display "`:env USERNAME'" |
display "`:env USERNAME'" |
||
display "`:env USERPROFILE'"</ |
display "`:env USERPROFILE'"</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">print("USER: \(ProcessInfo.processInfo.environment["USER"] ?? "Not set")") |
||
print("PATH: \(ProcessInfo.processInfo.environment["PATH"] ?? "Not set")")</ |
print("PATH: \(ProcessInfo.processInfo.environment["PATH"] ?? "Not set")")</syntaxhighlight> |
||
=={{header|True BASIC}}== |
=={{header|True BASIC}}== |
||
< |
<syntaxhighlight lang="qbasic">ASK #1: ACCESS type$ |
||
ASK BACK red |
ASK BACK red |
||
ASK COLOR red |
ASK COLOR red |
||
Line 1,194: | Line 1,194: | ||
ASK TEXT JUSTIFY vble1$, vble2$ |
ASK TEXT JUSTIFY vble1$, vble2$ |
||
ASK WINDOW vble1, vble2, vble3, vble4 |
ASK WINDOW vble1, vble2, vble3, vble4 |
||
ASK #4: ZONEWIDTH vble27</ |
ASK #4: ZONEWIDTH vble27</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
The <code>env</code> global array maps environmental variable names to their values: |
The <code>env</code> global array maps environmental variable names to their values: |
||
<lang |
<syntaxhighlight lang="tcl">$env(HOME)</syntaxhighlight> |
||
=={{header|TXR}}== |
=={{header|TXR}}== |
||
TXR can treat the environment vector as text stream: |
TXR can treat the environment vector as text stream: |
||
< |
<syntaxhighlight lang="txr">@(next :env) |
||
@(collect) |
@(collect) |
||
@VAR=@VAL |
@VAR=@VAL |
||
@(end)</ |
@(end)</syntaxhighlight> |
||
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): |
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): |
||
< |
<syntaxhighlight lang="txr">@(next :env) |
||
@(gather) |
@(gather) |
||
HOME=@home |
HOME=@home |
||
USER=@user |
USER=@user |
||
PATH=@path |
PATH=@path |
||
@(end)</ |
@(end)</syntaxhighlight> |
||
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: |
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: |
||
< |
<syntaxhighlight lang="txr">@(next :env) |
||
@(gather :vars (home user path (shell "/bin/sh"))) |
@(gather :vars (home user path (shell "/bin/sh"))) |
||
HOME=@home |
HOME=@home |
||
Line 1,220: | Line 1,220: | ||
PATH=@path |
PATH=@path |
||
SHELL=@shell |
SHELL=@shell |
||
@(end)</ |
@(end)</syntaxhighlight> |
||
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. |
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. |
||
< |
<syntaxhighlight 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"</ |
("/home/kaz" "kaz" "/home/kaz/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/kaz/bin"</syntaxhighlight> |
||
Here, the hash is being used as a function to filter several environment keys to their values via <code>mapcar</code>. |
Here, the hash is being used as a function to filter several environment keys to their values via <code>mapcar</code>. |
||
Line 1,233: | Line 1,233: | ||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
In the Unix Shell Language, environment variables are available as ordinary variables: |
In the Unix Shell Language, environment variables are available as ordinary variables: |
||
< |
<syntaxhighlight lang="bash">echo "$HOME"</syntaxhighlight> |
||
An ordinary variable can be marked as an environment variable with the <code>export</code> command: |
An ordinary variable can be marked as an environment variable with the <code>export</code> command: |
||
<lang |
<syntaxhighlight lang="bash">export VAR</syntaxhighlight> |
||
Now child processes launched by the shell will have an environment variable called <code>VAR</code>. |
Now child processes launched by the shell will have an environment variable called <code>VAR</code>. |
||
Line 1,242: | Line 1,242: | ||
=={{header|Ursa}}== |
=={{header|Ursa}}== |
||
< |
<syntaxhighlight lang="ursa">import "system" |
||
out (system.getenv "HOME") endl console</ |
out (system.getenv "HOME") endl console</syntaxhighlight> |
||
=={{header|Ursala}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="ursala">#import std |
||
#executable ('parameterized','') |
#executable ('parameterized','') |
||
showenv = <.file$[contents: --<''>]>+ %smP+ ~&n-={'TERM','SHELL','X11BROWSER'}*~+ ~environs</ |
showenv = <.file$[contents: --<''>]>+ %smP+ ~&n-={'TERM','SHELL','X11BROWSER'}*~+ ~environs</syntaxhighlight> |
||
The rest of this application searches for the three variables named |
The rest of this application searches for the three variables named |
||
and displays them on standard output. |
and displays them on standard output. |
||
Line 1,264: | Line 1,264: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
< |
<syntaxhighlight lang="vlang">// Environment variables in V |
||
// v run environment_variables.v |
// v run environment_variables.v |
||
module main |
module main |
||
Line 1,273: | Line 1,273: | ||
print('In the $os.environ().len environment variables, ') |
print('In the $os.environ().len environment variables, ') |
||
println('\$HOME is set to ${os.getenv('HOME')}') |
println('\$HOME is set to ${os.getenv('HOME')}') |
||
}</ |
}</syntaxhighlight> |
||
{{out}}<pre>prompt$ v run environment-variables.v |
{{out}}<pre>prompt$ v run environment-variables.v |
||
Line 1,279: | Line 1,279: | ||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
< |
<syntaxhighlight lang="vedit">Get_Environment(10,"PATH") |
||
Message(@10)</ |
Message(@10)</syntaxhighlight> |
||
Or with short keywords: |
Or with short keywords: |
||
< |
<syntaxhighlight lang="vedit">GE(10,"PATH") M(@10)</syntaxhighlight> |
||
=={{header|Visual Basic}}== |
=={{header|Visual Basic}}== |
||
{{works with|Visual Basic|VB6 Standard}} |
{{works with|Visual Basic|VB6 Standard}} |
||
< |
<syntaxhighlight lang="vb">Debug.Print Environ$("PATH")</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Line 1,292: | Line 1,292: | ||
However, if Wren is embedded in (say) a suitable Go program, then we can ask the latter to obtain it for us. |
However, if Wren is embedded in (say) a suitable Go program, then we can ask the latter to obtain it for us. |
||
< |
<syntaxhighlight lang="ecmascript">/* environment_variables.wren */ |
||
class Environ { |
class Environ { |
||
foreign static variable(name) |
foreign static variable(name) |
||
} |
} |
||
System.print(Environ.variable("SHELL"))</ |
System.print(Environ.variable("SHELL"))</syntaxhighlight> |
||
which we embed in the following Go program and run it. |
which we embed in the following Go program and run it. |
||
{{libheader|WrenGo}} |
{{libheader|WrenGo}} |
||
< |
<syntaxhighlight lang="go">/* environment_variables.go */ |
||
package main |
package main |
||
Line 1,325: | Line 1,325: | ||
vm.InterpretFile(fileName) |
vm.InterpretFile(fileName) |
||
vm.Free() |
vm.Free() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,338: | Line 1,338: | ||
version work the same as the 16-bit real-mode versions. |
version work the same as the 16-bit real-mode versions. |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations |
||
string 0; \use zero-terminated strings |
string 0; \use zero-terminated strings |
||
int CpuReg, PspSeg, EnvSeg, I, J, C; |
int CpuReg, PspSeg, EnvSeg, I, J, C; |
||
Line 1,365: | Line 1,365: | ||
if Peek(EnvSeg,I) = 0 then quit; \double 0 = env. var. not found |
if Peek(EnvSeg,I) = 0 then quit; \double 0 = env. var. not found |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,380: | Line 1,380: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">System.getenv("HOME") |
||
/home/craigd |
/home/craigd |
||
System.getenv() //--> Dictionary of all env vars</ |
System.getenv() //--> Dictionary of all env vars</syntaxhighlight> |