Environment variables
From Rosetta Code
Show how to get one of your process's environment variables. The available variables vary by system; some of the common ones available on Unix include PATH, HOME, USER.
[edit] Ada
Print a single environment variable.
with Ada.Environment_Variables; use Ada.Environment_Variables;
with Ada.Text_Io; use Ada.Text_Io;
procedure Print_Path is
begin
Put_Line("Path : " & Value("PATH"));
end Print_Path;
Print all environment variable names and values.
with Ada.Environment_Variables; use Ada.Environment_Variables;
with Ada.Text_Io; use Ada.Text_Io;
procedure Env_Vars is
procedure Print_Vars(Name, Value : in String) is
begin
Put_Line(Name & " : " & Value);
end Print_Vars;
begin
Iterate(Print_Vars'access);
end Env_Vars;
[edit] ALGOL 68
Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386 - getenv is not part of the standard's prelude
print((getenv("HOME"), new line))
[edit] AutoHotkey
EnvGet, OutputVar, Path
MsgBox, %OutputVar%
[edit] AWK
The ENVIRON array contains the values of the current environment:
$ awk 'BEGIN{print "HOME:"ENVIRON["HOME"],"USER:"ENVIRON["USER"]}'
HOME:/home/suchrich USER:SuchRich
Environment variables can also be assigned to awk variables before execution, with (-v) options:
$ awk -v h=$HOME -v u=$USER 'BEGIN{print "HOME:"h,"USER:"u}'
HOME:/home/suchrich USER:SuchRich
[edit] BASIC
x$ = ENVIRON$("path")
PRINT x$
[edit] C
#include <stdlib.h>
#include <stdio.h>
int main() {
puts(getenv("HOME"));
return 0;
}
[edit] C++
#include <cstdlib>
#include <cstdio>
int main()
{
puts(getenv("HOME"));
return 0;
}
[edit] C#
using System;
namespace RosettaCode {
class Program {
static void Main() {
string temp = Environment.GetEnvironmentVariable("TEMP");
Console.WriteLine("TEMP is " + temp);
}
}
}
[edit] Clojure
(System/getenv "HOME")
[edit] D
import tango.sys.Environment;
void main()
{
auto home = Environment("HOME");
}
[edit] 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
(lispworks:environment-variable "USER")
Works with: SBCL
(sb-ext:posix-getenv "USER")
Works with: Clozure CL
(ccl:getenv "USER")
Ways to do this in some other implementations are listed in the Common Lisp Cookbook.
[edit] E
Works with: E-on-Java
<unsafe:java.lang.System>.getenv("HOME")
[edit] Emacs Lisp
(getenv "HOME")
[edit] Factor
"HOME" os-env print
[edit] Forth
Works with: GNU Forth
s" HOME" getenv type
[edit] Fortran
Works with: any Fortran compiler
program show_home
implicit none
character(len=32) :: home_val ! The string value of the variable HOME
integer :: home_len ! The actual length of the value
integer :: stat ! The status of the value:
! 0 = ok
! 1 = variable does not exist
! -1 = variable is not long enought to hold the result
call get_environment_variable('HOME', home_val, home_len, stat)
if (stat == 0) then
write(*,'(a)') 'HOME = '//trim(home_val)
else
write(*,'(a)') 'No HOME to go to!'
end if
end program show_home
[edit] 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
[edit] J
2!:5'HOME'
[edit] Java
System.getenv("HOME") // get env var
System.getenv() // get the entire environment as a Map of keys to values
[edit] JavaScript
The JavaScript language has no facilities to access the computer: it relies on the host environment to provide it.
Works with: JScript
var shell = new ActiveXObject("WScript.Shell");
var env = shell.Environment("PROCESS");
WScript.echo('SYSTEMROOT=' + env.item('SYSTEMROOT'));
[edit] Joy
"HOME" getenv.
[edit] Objective-C
[[NSProcessInfo processInfo] environment] returns an NSDictionary of the current environment.
[[[NSProcessInfo processInfo] environment] objectForKey:@"HOME"]
[edit] OCaml
Sys.getenv "HOME"
[edit] Oz
{System.showInfo "This is where Mozart is installed: "#{OS.getEnv 'OZHOME'}}
[edit] Perl
The %ENV hash maps environment variables to their values:
print $ENV{HOME}, "\n";
[edit] Perl 6
Works with: Rakudo version #24 "Seoul"
The %*ENV hash maps environment variables to their values:
say %*ENV<HOME>;
[edit] PHP
The $_ENV associative array maps environmental variable names to their values:
$_ENV['HOME']
[edit] PicoLisp
: (sys "TERM")
-> "xterm"
: (sys "SHELL")
-> "/bin/bash"
[edit] PowerShell
Environment variables can be found in the Env: drive and are accessed using a special variable syntax:
$Env:Path
To get a complete listing of all environment variables one can simply query the appropriate drive for its contents:
Get-ChildItem Env:
[edit] PureBasic
PureBasic has the built in funtion
GetEnvironmentVariable("Name")
Example
If OpenConsole()
PrintN("Path:"+#CRLF$ + GetEnvironmentVariable("PATH"))
PrintN(#CRLF$+#CRLF$+"NUMBER_OF_PROCESSORS= "+ GetEnvironmentVariable("NUMBER_OF_PROCESSORS"))
PrintN(#CRLF$+#CRLF$+"Press Enter to quit.")
Input()
CloseConsole()
EndIf
[edit] Python
The os.environ dictionary maps environmental variable names to their values:
import os
os.environ['HOME']
[edit] R
Sys.getenv("PATH")
[edit] REBOL
print get-env "HOME"
[edit] Ruby
The ENV hash maps environmental variable names to their values:
ENV['HOME']
[edit] Slate
Environment variables at: 'PATH'.
"==> '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games'"
[edit] Standard ML
OS.Process.getEnv "HOME"
returns an option type which is either SOME value or NONE if variable doesn't exist
[edit] Tcl
The env global array maps environmental variable names to their values:
$env(HOME)
[edit] UNIX Shell
In Bash, you can use the environment variable like other variables in Bash; for example to print it out, you can do
echo $HOME
In Bash, the "env" command will print out all the key=value pairs to the screen.
[edit] 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.
#import std
#executable ('parameterized','')
showenv = <.file$[contents: --<''>]>+ %smP+ ~&n-={'TERM','SHELL','X11BROWSER'}*~+ ~environs
The rest of this application searches for the three variables named and displays them on standard output. Here is a bash session.
$ showenv < 'TERM': 'Eterm', 'SHELL': '/bin/bash', 'X11BROWSER': '/usr/bin/firefox'>
[edit] Vedit macro language
Get_Environment(10,"PATH")
Message(@10)
Or with short keywords:
GE(10,"PATH") M(@10)







