Environment variables: Difference between revisions
Underscore (talk | contribs) (Perl 5: Miscellaneous cleanup. Perl 6: Added.) |
(added Clojure version) |
||
Line 65: | Line 65: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|Clojure}}== |
|||
<lang lisp> |
|||
(System/getenv "HOME") |
|||
</lang> |
|||
=={{header|D}}== |
=={{header|D}}== |
Revision as of 01:58, 18 January 2010
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
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.
Ada
Print a single environment variable. <lang ada>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;</lang> Print all environment variable names and values. <lang ada>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;</lang>
ALGOL 68
<lang algol68>print((getenv("HOME"), new line))</lang>
AutoHotkey
<lang autohotkey>EnvGet, OutputVar, Path MsgBox, %OutputVar%</lang>
AWK
The ENVIRON array contains the values of the current environment: <lang awk>$ awk 'BEGIN{print "HOME:"ENVIRON["HOME"],"USER:"ENVIRON["USER"]}' HOME:/home/suchrich USER:SuchRich</lang> Environment variables can also be assigned to awk variables before execution, with (-v) options: <lang awk>$ awk -v h=$HOME -v u=$USER 'BEGIN{print "HOME:"h,"USER:"u}' HOME:/home/suchrich USER:SuchRich</lang>
BASIC
<lang qbasic>x$ = ENVIRON$("path") PRINT x$ </lang>
C
<lang c>#include <stdlib.h>
- include <stdio.h>
int main() {
puts(getenv("HOME")); return 0;
}</lang>
C#
<lang csharp>using System;
namespace RosettaCode {
class Program { static void Main() { string temp = Environment.GetEnvironmentVariable("TEMP"); Console.WriteLine("TEMP is " + temp); } }
}</lang>
Clojure
<lang lisp> (System/getenv "HOME") </lang>
D
<lang d>import tango.sys.Environment; void main() {
auto home = Environment("HOME");
}</lang>
Common Lisp
Access to environment variables isn't a part of the Common Lisp standard, but most implementations provide some way to do it.
<lang lisp>(lispworks:environment-variable "USER")</lang>
<lang lisp>(sb-ext:posix-getenv "USER")</lang>
<lang lisp>(ccl:getenv "USER")</lang>
Ways to do this in some other implementations are listed in the Common Lisp Cookbook.
E
<lang e><unsafe:java.lang.System>.getenv("HOME")</lang>
Factor
<lang factor>"HOME" os-env print</lang>
Forth
<lang forth>s" HOME" getenv type</lang>
Haskell
<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</lang>
J
<lang j>2!:5'HOME'</lang>
Java
<lang java>System.getenv("HOME") // get env var System.getenv() // get the entire environment as a Map of keys to values</lang>
JavaScript
The JavaScript language has no facilities to access the computer: it relies on the host environment to provide it.
<lang javascript>var shell = new ActiveXObject("WScript.Shell"); var env = shell.Environment("PROCESS"); WScript.echo('SYSTEMROOT=' + env.item('SYSTEMROOT'));</lang>
Joy
<lang joy>"HOME" getenv.</lang>
Objective-C
[[NSProcessInfo processInfo] environment]
returns an NSDictionary of the current environment.
<lang objc>[[[NSProcessInfo processInfo] environment] objectForKey:@"HOME"]</lang>
OCaml
<lang ocaml>Sys.getenv "HOME"</lang>
Oz
<lang oz>{System.showInfo "This is where Mozart is installed: "#{OS.getEnv 'OZHOME'}}</lang>
Perl
The %ENV
hash maps environment variables to their values:
<lang perl>print $ENV{HOME}, "\n";</lang>
Perl 6
The %*ENV
hash maps environment variables to their values:
<lang perl6>say %*ENV<HOME>;</lang>
PHP
The $_ENV associative array maps environmental variable names to their values: <lang php>$_ENV['HOME']</lang>
PowerShell
Environment variables can be found in the Env: drive and are accessed using a special variable syntax: <lang powershell>$Env:Path</lang> To get a complete listing of all environment variables one can simply query the appropriate drive for its contents: <lang powershell>Get-ChildItem Env:</lang>
Python
The os.environ dictionary maps environmental variable names to their values: <lang python>import os os.environ['HOME']</lang>
R
<lang R>Sys.getenv("PATH")</lang>
REBOL
<lang REBOL>print get-env "HOME"</lang>
Ruby
The ENV hash maps environmental variable names to their values: <lang ruby>ENV['HOME']</lang>
Slate
<lang slate>Environment variables at: 'PATH'. "==> '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games'"</lang>
Standard ML
<lang sml>OS.Process.getEnv "HOME"</lang> returns an option type which is either SOME value or NONE if variable doesn't exist
Tcl
The env
global array maps environmental variable names to their values:
<lang tcl>$env(HOME)</lang>
UNIX Shell
In Bash, you can use the environment variable like other variables in Bash; for example to print it out, you can do <lang bash>echo $HOME</lang> In Bash, the "env" command will print out all the key=value pairs to the screen.
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. <lang Ursala>#import std
- executable ('parameterized',)
showenv = <.file$[contents: --<>]>+ %smP+ ~&n-={'TERM','SHELL','X11BROWSER'}*~+ ~environs</lang> 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'>
Vedit macro language
<lang vedit>Get_Environment(10,"PATH") Message(@10)</lang>
Or with short keywords: <lang vedit>GE(10,"PATH") M(@10)</lang>