Environment variables

From Rosetta Code
Revision as of 22:44, 5 August 2009 by Eriksiers (talk | contribs) (added BASIC)
Task
Environment variables
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

Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386 - getenv is not part of the standard's prelude

<lang algol>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>

  1. 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>

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.

Works with: LispWorks

<lang lisp>(lispworks:environment-variable "USER")</lang>

Works with: SBCL

<lang lisp>(sb-ext:posix-getenv "USER")</lang>

Works with: Clozure CL

<lang lisp>(ccl:getenv "USER")</lang>

Ways to do this in some other implementations are listed in the Common Lisp Cookbook.

E

Works with: E-on-Java

<lang e><unsafe:java.lang.System>.getenv("HOME")</lang>

Forth

Works with: GNU Forth
s" HOME" getenv type

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

J

   2!:5'HOME'

Java

<lang java>System.getenv("HOME") // get env var System.getenv() // get the entire environment as a Map of keys to values</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>

Perl

The %ENV hash maps environmental variable names to their values: <lang perl>$ENV{'HOME'}</lang>

PHP

The $_ENV associative array maps environmental variable names to their values: <lang php>$_ENV['HOME']</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>

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

echo $HOME

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

  1. 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>